so I have a python script which takes the filename as a command argument and processes that file. However, because I have 263 files which need the same processing, I was wondering whether the command argument section could be modified with a for loop to consecutively run through all the files in a folder? Cheers, Sat
EDIT:
The code for the system argument is here:
try:
opt_list, args = getopt.getopt(sys.argv[1:], 'r:vo:A:Cp:U:eM:')
except getopt.GetoptError, msg:
print 'prepare_receptor4.py: %s' %msg
usage()
sys.exit(2)
with ‘r’ being the name of the file needing to be processed and the others are optional arguments. I’m not sure how to modify this with a for loop.
As a practical matter, whatever shell you’re using probably has some syntax that can be easily used for this. In Bash, for example:
To actually do this in Python, I’d suggest structuring your program so that the main code is in a function which takes one argument, the filename.
Then you can invoke this function like so,
foldercould be passed as a command-line argument, or just written into the script (if it’s not something you’d be reusing).EDIT: In response to your edit, I’d suggest just giving the filenames as regular command-line arguments, without using the
-roption, so that they’ll wind up inargs. Then you can door if you would rather pass the directory name as the command-line argument,
Alternatively, I suppose you could pass multiple instances of the
-roption, and then do