What's the problem with test.py code?
$ ls *csv
test1.csv test2.csv test3.csv test4.csv
$ python test.py *csv
test1.csv
$ python test.py '*csv'
test1.csv
test2.csv
test3.csv
test4.csv
$ cat test.py
#!/usr/bin/python
import sys,glob
for filename in glob.glob(sys.argv[1]):
print filename
$
Ultimately I want not to be forced to pass arguments to the script with quotes.
EDIT: found the problem after comments on the accepted answer
If shell expansion works, I don't have to use glob, so the loop is:
for filename in sys.argv[1:]:
print filename
Thanks!
This isn't anything specific to glob
or Python, it's just how wildcards work in the shell. You can see this by replacing your script with one that just does:
import sys
print sys.argv
In the first example, the *.csv
is expanded into multiple filenames by the shell before your script runs. You can see this by printing sys.argv
- all of the matching filenames will be in there.
If you don't want the shell to expand wildcards then you need to quote or escape them using \
. (There is one corner case here - bash will pass *.csv
in to your script if there are no files that match. zsh will signal an error in this case by default.)
Apparently you can turn off glob expansion in the shell with:
set -f
But I don't think that's what you want.
Lots of information about expansion here.