Python: iterate (and read) all files in a directory (folder)
12th August 2007
I found a sample of Python code to iterate through all the files within the specified folder (directory), with ability to use wildcards (*, ?, and [ ]-style ranges). Below is a portion of code from a working script:
-
import os, glob
-
-
path = 'sequences/'
-
for infile in glob.glob( os.path.join(path, '*.fasta') ):
-
print "current file is: " + infile
-
# for python 3.0 and above, print is now a function print():
-
# print("current file is: " + infile)
Thanks Dt for mentioning print being promoted from a statement to a function.
Clearly, the only reason to use that 'os.path.join()' part is to make the script cross-platform-portable, as different systems use different path separators, and hard-coding path separator would stop the script from executing under a different OS.
Python docs mention that there is also iglob(), which is an iterator and thus working on directories with way too many files it will save memory by returning only single result per iteration, and not the whole list of files - as glob() does.


















March 22nd, 2008 at 20:13
This stuff isnt working on my windows system
March 23rd, 2008 at 12:36
it should. you might want to modify the example to suit your exact needs - this example assumes there is 'sequences' folder with at least one .fasta file - if that's not true, then - evidently - you are not going to see this script work, be it windows or linux.
December 23rd, 2008 at 11:38
works just fine for me, only important change to the code that i had to make was turning print into a function because im using python 3.0, i also set it to read files with *all* extensions.
import os, glob
path = 'insert your own path you lazy bastards '
for infile in glob.glob( os.path.join(path, '*.*') ):
print("current file is: " + infile)
December 23rd, 2008 at 13:21
Dt, thanks, I've updated the code.