I need to get an unknown number of file names from the command line, ie
./python MyScript.py --Files 1.txt 2.txt 3.txt

parser.add_option("-n", "--NumFiles", type="int",
                  help="Number of files")
parser.add_option("--Files", nargs=options.NumFiles,
                  help="a triple")
(options, args) = parser.parse_args()

Clearly this will not work because options.NumFiles is not available until after parse_args() is called. Is there a way to do this?

Thanks,
Dave

What I'm really trying to do is loop though a list of files

python ./MyScript.py *.jpg

should be able to do something like this

for MyFile in *.jpg
   print i

so I can operate on each file. How would I do that?

Dave

If that is all of the arguments your program will take, this would be the easiest option:

import sys

for file_name in sys.argv[1:]:
    print file_name

thanks, that's a good start for now, but eventually it would be nice to do

--files *.txt --Size 4.2 --Duration 5

Dave

thanks, that's a good start for now, but eventually it would be nice to do

--files *.txt --Size 4.2 --Duration 5

Dave

I'm not entirely clear on what you're trying to do, but if you want to get all files in a directory that are *.txt you can use the glob module:

>>> import os,glob
>>> os.chdir('C:\\Documents and Settings\\Administrator\\Desktop')
>>> my_files = glob.glob('*.py')
>>> my_files
['batt.py', 'newt_root.py', 'perf_num.py', 'template.py']
>>>

EDIT: I'm sure that you're not actually restricted to a single directory since you can probably use wild cards for the directory too. I think glob just expands exactly the way that Unix path expansion works.

HTH

This question has already been answered. Start a new discussion instead.