Search code examples
pythonoptparse

Python, optparse and file mask


if __name__=='__main__':
    parser = OptionParser()
    parser.add_option("-i", "--input_file", 
                    dest="input_filename",
                      help="Read input from FILE", metavar="FILE")

    (options, args) = parser.parse_args()
    print options

result is

$ python convert.py -i video_*
{'input_filename': 'video_1.wmv'}

there are video_[1-6].wmv in the current folder. Question is why video_* become video_1.wmv. What i'm doing wrong?


Solution

  • Python has nothing to do with this -- it's the shell.

    Call

    $ python convert.py -i 'video_*'
    

    and it will pass in that wildcard.

    The other six values were passed in as args, not attached to the -i, exactly as if you'd run python convert.py -i video_1 video_2 video_3 video_4 video_5 video_6, and the -i only attaches to the immediate next parameter.

    That said, your best bet might to be just read your input filenames from args, rather than using options.input.