Python、optparse 和文件掩码
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
结果是
$ python convert.py -i video_*
{'input_filename': 'video_1.wmv'}
当前文件夹中有video_[1-6].wmv。 问题是为什么 video_* 变成 video_1.wmv。 我做错了什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Python 与此无关——它是 shell。
调用
它就会传入该通配符。
其他六个值作为参数传入,未附加到
-i
,就像运行python Convert.py -i video_1 video_2 video_3 video_4 video_5 video_6
,并且-i
仅附加到紧邻的下一个参数。也就是说,最好的选择可能是从 args 读取输入文件名,而不是使用 options.input 。
Python has nothing to do with this -- it's the shell.
Call
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 runpython 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 usingoptions.input
.打印出 args,您将看到其他文件的去向...
它们将被转换为 argv 中的单独参数,而 optparse 仅将第一个文件作为 input_filename 选项的值。
Print out args and you'll see where the other files are going...
They are being converted to separate arguments in argv, and optparse only takes the first one as the value for the input_filename option.
澄清一下:
在 Linux shell 上,所有通配符 (*.wmv) 都会由 shell 扩展。 所以
aprogram
实际上接收了参数:就像Charles也就是说,您可以引用参数以使其按字面意义传递:
这将传递:
To clarify:
on a Linux shell, all wildcards (*.wmv) are expanded by the shell. So
aprogram
actually recieves the arguments:Like Charles said, you can quote the argument to get it to pass in literally:
This will pass in:
即使您阅读了一些标准(例如 this< /a> 或此)。
命令行的args部分几乎都是输入文件。
只有非常罕见的奇怪情况才会将输入文件指定为选项。 这种情况确实会发生,但非常罕见。
此外,输出文件永远不会命名为args。 它们几乎总是作为命名选项提供。
这个想法是
-
的命令行参数是“stdin”的代码。 如果没有给出参数,stdin 是后备计划。如果您的程序打开任何文件,它也可以打开命令行上指定的无限数量的文件。 shell 通过为您扩展通配符来实现这一点。 [但是,Windows 不会为您执行此操作。]
如果没有明确的命令行选项(例如用于写入文件的“-o somefile”),您的程序绝对不应该覆盖文件。
请注意,
cp
、mv
、rm
是不遵循这些标准的程序的重要示例。It isn't obvious, even if you read some of the standards (like this or this).
The args part of a command line are -- almost universally -- the input files.
There are only very rare odd-ball cases where an input file is specified as an option. It does happen, but it's very rare.
Also, the output files are never named as args. They almost always are provided as named options.
The idea is that
Most programs can (and should) read from stdin. The command-line argument of
-
is a code for "stdin". If no arguments are given, stdin is the fallback plan.If your program opens any files, it may as well open an unlimited number of files specified on the command line. The shell facilitates this by expanding wild-cards for you. [Windows doesn't do this for you, however.]
You program should never overwrite a file without an explicit command-line options, like '-o somefile' to write to a file.
Note that
cp
,mv
,rm
are the big examples of programs that don't follow these standards.