Prince Patel
Prince Patel

Reputation: 302

Python argv taking wild card path

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

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)]

Upvotes: 3

Views: 2393

Answers (2)

al2
al2

Reputation: 159

As @Jean-François Fabre sais, it is possible to use wildcards on windows using glob module.

import glob
paths = glob.glob('C:\path\with\wildcard.*')

Upvotes: 3

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476594

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.

If you write:

python file.py 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.

You should use quotes to pass the asterisk, like:

python file.py 'doc/*.png'

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

Upvotes: 7

Related Questions