Skip to content

Instantly share code, notes, and snippets.

@gregcaporaso
Created November 24, 2012 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregcaporaso/4140516 to your computer and use it in GitHub Desktop.
Save gregcaporaso/4140516 to your computer and use it in GitHub Desktop.
Example of using glob to compile a list of filepaths
from glob import glob
filepaths = glob('*txt')
for filepath in filepaths:
f = open(filepath,'U')
# tip: always open files for reading with mode 'U' rather
# than mode 'r'
## Do whatever with the open file
f.close()
# if files of interest are all in subdirectories of the
# current working directory, something like this would work
filepaths = glob('*/*txt')
for filepath in filepaths:
f = open(filepath,'U')
# tip: always open files for reading with mode 'U' rather
# than mode 'r'
## Do whatever with the open file
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment