Member Avatar for Rebecca_2

In the parent directory O, I have a series of directories Aa, where a varies from 0-1. In each of these I have a series of directories Bb, where b varies from 0-1. In each of these there are the input/output files AaBbCc.dat and AaBbCc.out for some calculations [c also varies from 0-1]. However, due to a limit on the amount of resources I can use some of these calculations failed to complete (or even run).

I have created a file 'walltime.txt' which lists the output files [AaBbCc.out] that have closed prior to the calculation finishing. It is from here that the variables a, b and c take there values.

I am trying to write a program which, overall takes the names of the files that haven't completed, works out the names of those that haven't run and writes all of these into a batch file.

i.e.
The filename A0.8B0.6C0.9.out appears in 'walltime.txt'.
As such: inp = A0.8B0.6C0.9.dat; outp = A0.8B0.6C0.9.out; a = 0.8; b = 0.6 and c = 0.9 The list GULP is then formed: GULP = ['gulp < ' + inp + ' > ' + outp]

N.B. As I will want to add to this, I am assuming it cannot just be a variable, but a one item list.

In order to work out if there are any files in the directory that haven't run, I need to do the following [which was the original post]:

When c does not equal 1.00, extend the list GULP to include gulp < ' + AaBbCc.dat + ' > ' + AaBbCc.out. This must always happen, except when a == 0.3 and b == 0.2 and when a == 0.8 and c == 0.6.

Does this actually make any logical sense?

If so, how do I go about implementing such a piece of code in python 3: I know I am going to need an if loop for the first bit, but am unsure as to how to build in the exception.

I found literature on 'except', but this seemed to be more designed for a 'trial and improvement' methodology, and about catching errors that occur in programs. Would it still be possible for me to implement the except function?

I feel I may totally misunderstand your question.

However, if I do understand it I would first create a list of all the files in the directory you intend to run.

files = os.listdir()

then go to the files in the created directory.
Use try except to test and if failed move to batch file.

try:
   with open('filename'):
       process()
except IOError:
   # move fails to batch file

I am trying to write a program which, overall takes the names of the files that haven't completed, works out the names of those that haven't run and writes all of these into a batch file.

I would agree. You are perhaps making it too complicated. Write/store the file name when it's processing has finished. If a file in a directory is not in the finished list then process it.

When c does not equal 1.00, extend the list GULP to include gulp < ' + AaBbCc.dat + ' > ' + AaBbCc.out. This must always happen, except when a == 0.3 and b == 0.2 and when a == 0.8 and c == 0.6.

You can extend the list by all names in a directory "except when a == 0.3 and b == 0.2 and when a == 0.8 and c == 0.6" and not have to worry about which ones have run. If the program exits before all files have run then the program name is not in the stored list and will run next time this program is run.

If I understand what you want to do, you will use something like the subprocess modules to run the "bash" commands and when it finishes, and returns to the calling program, write the name of the file to where ever it is being stored.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.