在Windows中将带有星号的多个文件传递给python shell [英] Passing multiple files with asterisk to python shell in Windows

查看:104
本文介绍了在Windows中将带有星号的多个文件传递给python shell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆