I am really stumped on this one.
I have a simple python wrapper which looks something like this:
import glob
for found in glob.glob(filename):
if not os.path.isdir(found):
my_module.do_stuff(found)
where filename has been read in from sys.argv.
When I try glob in an interactive shell, or a hello world script, I get the full list of (in this case 5) files. However, when I use it in this context, I only get the first one (alphabetically).
I’ve checked by catching the result of glob.glob in an array and sure enough, it’s only got a len() of 1, even when the filename is just '*'.
What could I possibly be doing that breaks glob?!
Full code file, just in case you can spot my gotcha:
#! /usr/bin/python
import pynet.quadrons as q
import os, glob
def print_usage():
print """
(blah blah big long string.)
"""
if __name__ == "__main__":
import sys
if len(sys.argv) < 2:
print_usage()
exit()
filename = ''
try:
filename = sys.argv[1]
except:
print "error parsing arguments."
print_usage()
exit()
for found in glob.glob(filename):
if not os.path.isdir(found):
q.load_and_analyse_file(found)
The shell is expanding the glob before your Python script sees it. Therefore your Python script sees the first full filename that matches the glob in
sys.argv[1], passes that toglob(), and of course it only matches one file.Either quote the argument in the shell with single quotes so that the shell does not expand it, or simply allow the shell to do the expanding and iterate over all items of
sys.argv(except the first).It’s worthwhile to note that on Windows, the shell doesn’t do globbing, so if your script needs to work cross-platform, you should iterate over
sys.argv[1:]and glob each item.