Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python's glob only returning the first result

Tags:

python

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)
like image 433
tehwalrus Avatar asked Dec 16 '11 18:12

tehwalrus


People also ask

Does glob return a list?

glob() method returns a list of files or folders that matches the path specified in the pathname argument.

Is glob faster than OS Listdir?

listdir is quickest of three. And glog. glob is still quicker than os.

Does glob use regex?

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.

Is glob glob recursive?

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.


1 Answers

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.

like image 78
kindall Avatar answered Sep 19 '22 05:09

kindall