To iterate through all the files within the specified directory (folder), with ability to use wildcards (*, ?, and [ ]-style ranges), use the following code snippet:
PYTHON:
- import os
- import glob
- path = 'sequences/'
- for infile in glob.glob( os.path.join(path, '*.fasta') ):
- print "current file is: " + infile
If you do not need wildcards, then there is a simpler way to list all items in a directory:
PYTHON:
- import os
- path = 'sequences/'
- listing = os.listdir(path)
- for infile in listing:
- print "current file is: " + infile
print was promoted from a statement to a function in Python 3 (use print(infile) instead of print infile).
One should use 'os.path.join()' part to make the script cross-platform-portable (different OS 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.