Listing Files with Python
Thursday, April 26th, 2007Getting a list of files in a directory and its sub directories is very simple using the built in os module in Python. In this example the file can be run with the directory to explore as the command line argument. Any directories found in the given directory will be recursed on and their files added the final list of all files.
# Emgarten.com April 26th, 2007
import os,sys
def findFiles(dir):
files = []
# remove a trailing slash if it exists
if dir[-1:] == “/”:
dir = dir[0:-1]
# loop through files and directories
for x in os.listdir(dir):
if os.path.isdir(dir + “/” + x):
# list this dir also
files.extend(findFiles(dir + “/” + x))
else:
# add the file to the list
files.append(dir + “/” + x)
return files
if __name__ == “__main__”:
if len(sys.argv) != 2:
print “Usage: %s <dir>” % sys.argv[0]
sys.exit(1)
# get the list of files
files = findFiles(sys.argv[1])
for file in files:
print file
The function returns a list with the full path of all files found in the directory.