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

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

in the current folder there is video_ [1-6] .wmv. The question is why video_ * became video_1.wmv. What am I doing wrong?

+3
source share
4 answers

Python has nothing to do with it - it's a wrapper.

Call

$ python convert.py -i 'video_*'

and it will be held in this template.

The remaining six values ​​were passed as args, not bound to -i, in the same way as if you were running python convert.py -i video_1 video_2 video_3 video_4 video_5 video_6, but -ibound only to the next next parameter.

However, the best choice is to simply read your input file names from args, rather than use options.input.

+8
source

Print out the arguments and you will see where the other files go ...

argv, optparse input_filename.

+2

:

aprogram -e *.wmv

Linux, (*.wmv) . , aprogram :

sys.argv == ['aprogram', '-e', '1.wmv', '2.wmv', '3.wmv']

, , :

aprogram -e "*.wmv"

:

sys.argv == ['aprogram', '-e', '*.wmv']
+1

, (, this this).

args - - .

, . , .

, args. .

,

  • ( ) stdin. - - "stdin". , stdin - .

  • , , . , . [Windows , .]

  • , '-o somefile' .

, cp, mv, rm - , .

0

All Articles