Reputation: 113
I'm really unexpert in python, so forgive my question if stupid.
I'm trying a simple script that operates on all the files in a folder. However, I apparently can only access the folder recursively!
I explain. I have a folder, DATA, with subfolders for each day (of the form YYYY-MM-DD).
If I try
for filename in glob.glob('C:\Users\My username\Documents\DATA\2021-01-20\*'):
print filename
I get no output.
However, if I try instead
for filename in glob.glob('C:\Users\My username\Documents\DATA\*\*'):
print filename
the output is that expected:
C:\Users\My username\Documents\DATA\2021-01-20\210120_HOPG_sputteredTip0001.sxm
C:\Users\My username\Documents\DATA\2021-01-20\210120_HOPG_sputteredTip0002.sxm
...
I even tried different folder names (removing the dashes, using letters in the beginning, using only letters, using a shorter folder name) but the result is still the same.
What am I missing?
(BTW: I am on python 2.7, and it's because the program I need for the data is only compatible with python 2)
Upvotes: 1
Views: 1930
Reputation: 528
Beware when using backslashes in strings. In Python this means escaping characters. Try prepending your string with r
like so:
for filename in glob.glob(r'C:\Users\My username\Documents\DATA\*'):
# Do you business
Edit:
As @poomerang has pointed out a shorter answer has previously been provided as to what 'r' does in Python here
Official docs for Python string-literals: Python 2.7 and for Python 3.8.
Upvotes: 1
Reputation: 3116
Recursive file search is not possible with glob
in Python 2.7. I.e. searching for files in a folder, its subfolders, sub-subfolders and so on.
You have two options:
os.walk
(you might need to change your code's structure however)pathlib2
module from PyPI https://pypi.org/project/pathlib2/ - which should include a glob
function supporting the recursive search using **
wildcard.Upvotes: 1