问题描述
我以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外壳程序(bash
,sh
,fish
,...)上,星号由外壳程序处理. *
转换为文件列表的事实已经在外壳程序级别上完成.
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采用通配符路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!