在Windows中将带有星号的多个文件传递给python shell [英] Passing multiple files with asterisk to python shell in Windows
问题描述
我正在经历Google的Python练习,我需要能够从命令行执行此操作:
I'm going through Google's Python exercises and I need to be able to do this from the command line:
python babynames.py --summaryfile baby*.html
其中python
是Python shell,babynames.py
是Python程序,--summaryfile
是要由我的babynames
程序解释的参数,而baby*.html
是与该表达式匹配的文件列表.但是,它不起作用,并且我不确定问题是否出在Windows命令外壳程序或Python. baby*.html
表达式不会扩展到完整的文件列表,而是严格作为字符串传递.可以通过这种方式将多个文件传递给Python程序吗?
Where python
is the Python shell, babynames.py
is the Python program, --summaryfile
is an argument to be interpreted by my babynames
program, and baby*.html
is the list of files matching that expression. However, it doesn't work and I'm not sure if the problem is the Windows command shell or Python. The baby*.html
expression is not being expanded out to the full list of files, instead it's being passed strictly as a string. Can multiple files be passed to a Python program in such a way?
推荐答案
Windows的命令解释器不会像在UNIX shell中那样将通配符传递给执行的程序或脚本之前,对通配符进行扩展.
Windows' command interpreter does not expand wildcards as UNIX shells do before passing them to the executed program or script.
python.exe -c "import sys; print sys.argv[1:]" *.txt
结果:
['*.txt']
解决方案:使用glob
模块.
from glob import glob
from sys import argv
for filename in glob(argv[1]):
print filename
这篇关于在Windows中将带有星号的多个文件传递给python shell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!