23

I'm trying to use glob and os to locate the most recent .zip file in a directory. Funny thing is, I had the following set up and it was working previously:

max(glob.glob('../directory/*.zip'), key=os.path.getctime)

Running this now gets me max() arg is an empty sequence, which makes sense because when I try this:

glob.glob('../directory/*.zip')

it returns nothing but an empty list. Using the full path also gets me an empty list. Trying other directories also gets me an empty list. I'm very confused about what's going on here given this worked perfectly previously. Help?

EDIT: Got it to work again using: glob.glob(/Users/*/directory/*.zip)

TheVideotapes
  • 383
  • 2
  • 3
  • 9
  • 5
    You're using a relative path, are you sure that it points to the same directory that you assume it does ? – Nir Alfasi Jun 03 '16 at 16:26
  • 3
    Agree with @alfasin. It sounds like the directory you are globbing just doesn't have `.zip` files in it. As a test to make sure something weird isn't happening where you get an empty list no matter what, you could just try `glob.glob('../directory/*')` or `glob.glob('*')`. If those return empty lists, something strange is going on. Otherwise you are probably just pointing to the wrong directory, or the directory doesn't have any `.zip` files in it. Try using an absolute path too. – elethan Jun 03 '16 at 16:30
  • If you hadn't mentioned that the full path also fails, I would assume you were simply running the script from a different directory. As it is, I can only assume you entered the wrong full path. What is the output of `os.listdir('../directory/')`? – chepner Jun 03 '16 at 16:32
  • @alfasin I've tried with the full path as well and an empty list is still returned. @elethan `glob.glob('*')` works perfectly @chepner I get "No such file or directory" but I know this directory exists because I can navigate to it – TheVideotapes Jun 03 '16 at 16:46
  • Well, I got it to work again using `glob.glob(/Users/*/directory/*.zip)`. I still don't understand why it worked previously though. – TheVideotapes Jun 03 '16 at 17:11

3 Answers3

11

You want the ** glob operator:

glob.glob('**/*.zip',recursive=True)

Will match all files ending in '.zip' in the current directory and in all subdirectories for example.

jb4earth
  • 198
  • 1
  • 6
6

In my case, I forgot to escape the special character [ that was in the directory name using glob.escape(pathname).

So instead of glob.glob(pathname), try glob.glob(glob.escape(pathname)).

Jordan Draper
  • 61
  • 1
  • 1
  • 2
    +rep, but don't escape the asterisks. e.g.: glob.glob( glob.escape(path) + '/*.jpg' ). The escaped asterisks don't cause glob action. Please, edit your post. – A. Genchev Apr 15 '21 at 20:57
  • I usually never use these special characters, but this was my client's case. Thanks. – HyeonPhil Youn Dec 16 '22 at 14:09
0

I faced a lot of problem in globbing on ubuntu. This code works fine on windows

cv_img = []
for img in glob.glob('/home/itisha/Desktop/op/*.JPG'):
    print('hi')
    n= cv2.imread(img)
    cv_img.append(n)

But for Ubuntu you have to replace line 2 by

for img in glob.glob('/home/*itisha/*Desktop/*op/*.JPG'):
itisha
  • 47
  • 5
  • On Ubuntu, there is no need of so many *. It will definitely work as you have written it worked for Windows. – SKR Oct 13 '18 at 03:50
  • 4
    glob.glob on my mac returns empty list if your path starts with '~'. It does not recognize this. – Tony Nov 05 '18 at 23:14
  • True, you must get full path, ie -> pathlib.Path.home and prepend it. – m3nda Feb 03 '22 at 23:20