Created
November 24, 2012 17:00
-
-
Save gregcaporaso/4140516 to your computer and use it in GitHub Desktop.
Example of using glob to compile a list of filepaths
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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