Python argv采用通配符路径 [英] Python argv taking wild card path

查看:56
本文介绍了Python argv采用通配符路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以doc1/*.png作为第一个参数运行脚本,但该脚本被转换为doc1/image1.png.

I run my script with doc1/*.png as first argument, but it gets converted to doc1/image1.png.

如何让Python看到确切的参数?

How can I let Python see the exact argument?

img_list = []
print sys.argv[1]
x = sys.argv[1]
img_list = [img for img in glob.glob(x)]

推荐答案

在大多数Linux外壳程序(bashshfish,...)上,星号由外壳程序处理. *转换为文件列表的事实已经在外壳程序级别上完成.

On most linux shells (bash, sh, fish,...), the asterisk is handled by the shell. The fact that the * is converted to a list of files is already done at the shell level.

如果您写:

python file.py doc/*.png

外壳本身会将doc/*.png转换为"doc/1.png" "doc/2.png"(因此,它在doc目录中找到的.png文件列表.

The shell itself will translate doc/*.png into "doc/1.png" "doc/2.png" (so a list of .png files it finds in the doc directory.

您应使用引号将星号传递,例如:

python file.py 'doc/*.png'

标准Windows shell 不对文件名使用通配符.

The standard Windows shell does not do wildcards for file names.

这篇关于Python argv采用通配符路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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