2

I saw this answer - How can I search sub-folders using glob.glob module in Python? but I didn't understand what os.walk() was doing (I read the docs but it didn't quite make sense).

I'm really new to pathing and still trying to make sense of it.

I have a script that lives in - /users/name/Desktop/folder/ and I want to access some files in /users/name/Desktop/folder/subfolder/*.html

I tried glob.glob('/users/name/Desktop/folder/subfolder/*.html') but it returned an empty list. I realize this is what the previous person did and it didn't work (I was just hoping that glob had been updated!)

Any thoughts on how to do this?

Community
  • 1
  • 1
Morgan Allen
  • 3,291
  • 8
  • 62
  • 86
  • Are the files in `subfolder` itself? The question you linked seems to be about recursive globbing: looking for every file that matches `*.html` in subfolders of subfolders of subfolders of subfolders (etc) of the path. If the files are actually in the folder called `subfolder`, and not a folder below that one, you don't need recursive globbing and the code you pasted should Just Work. – Wander Nauta Dec 21 '15 at 17:35
  • Is there a good way to make sure I'm doing the pathing right? I have my MainFolder folder with my script, and then two sub folders, Folder A & Folder B. I want to be in MainFolder and run the script that uses the files from Folder A. – Morgan Allen Dec 21 '15 at 17:41
  • You can try opening Terminal.app and doing `ls` followed by your path in double quotes (the entire thing including `*.html`). If that doesn't give any results, the files you're looking for are somewhere else. If it gives an error, your path is wrong, and bash will shout at you. If it does give results, the problem is with your script. – Wander Nauta Dec 21 '15 at 17:46

1 Answers1

3

Without any further information it's hard to say what the issue is. I tested your syntax, it works fine for me. Are you sure the extension is .html not .htm in your /users/name/Desktop/folder/subfolder/ directory?

Also, to further troubleshoot you can check what python can see in you directory by running:

os.listdir('/users/name/Desktop/folder/subfolder/')

This should get you started.

  • 1
    ahh, ok, using that I found my error :-) `folder` was actually `'longer name'` and I was used to doing `'longer\ name'` from the terminal. Once I removed the '\' it worked! – Morgan Allen Dec 21 '15 at 17:43