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)
glob() method returns a list of files or folders that matches the path specified in the pathname argument.
listdir is quickest of three. And glog. glob is still quicker than os.
The pattern rules for glob are not regular expressions. Instead, they follow standard Unix path expansion rules. There are only a few special characters: two different wild-cards, and character ranges are supported.
Using Glob() function to find files recursivelyWe can use the function glob. glob() or glob. iglob() directly from glob module to retrieve paths recursively from inside the directories/files and subdirectories/subfiles.
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 to glob()
, 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With