In this article we will discuss different methods to generate a list of all files in directory tree.
Creating a list of files in directory and sub directories using os.listdir()
Python’s os module provides a function to get the list of files or folder in a directory i.e.
os.listdir(path='.')
It returns a list of all the files and sub directories in the given path.
We need to call this recursively for sub directories to create a complete list of files in given directory tree i.e.
Frequently Asked:
''' For the given path, get the List of all files in the directory tree ''' def getListOfFiles(dirName): # create a list of file and sub directories # names in the given directory listOfFile = os.listdir(dirName) allFiles = list() # Iterate over all the entries for entry in listOfFile: # Create full path fullPath = os.path.join(dirName, entry) # If entry is a directory then get the list of files in this directory if os.path.isdir(fullPath): allFiles = allFiles + getListOfFiles(fullPath) else: allFiles.append(fullPath) return allFiles
Call the above function to create a list of files in a directory tree i.e.
dirName = '/home/varun/Downloads'; # Get the list of all files in directory tree at given path listOfFiles = getListOfFiles(dirName)
Creating a list of files in directory and sub directories using os.walk()
Python’s os module provides a function to iterate over a directory tree i.e.
os.walk(path)
It iterates of the directory tree at give path and for each directory or sub directory it returns a tuple containing,
(<Dir Name> , <List of Sub Dirs> , <List of Files>.
Iterate over the directory tree and generate a list of all the files at given path,
# Get the list of all files in directory tree at given path listOfFiles = list() for (dirpath, dirnames, filenames) in os.walk(dirName): listOfFiles += [os.path.join(dirpath, file) for file in filenames]
Complete example is as follows,
Latest Python - Video Tutorial
import os ''' For the given path, get the List of all files in the directory tree ''' def getListOfFiles(dirName): # create a list of file and sub directories # names in the given directory listOfFile = os.listdir(dirName) allFiles = list() # Iterate over all the entries for entry in listOfFile: # Create full path fullPath = os.path.join(dirName, entry) # If entry is a directory then get the list of files in this directory if os.path.isdir(fullPath): allFiles = allFiles + getListOfFiles(fullPath) else: allFiles.append(fullPath) return allFiles def main(): dirName = '/home/varun/Downloads'; # Get the list of all files in directory tree at given path listOfFiles = getListOfFiles(dirName) # Print the files for elem in listOfFiles: print(elem) print ("****************") # Get the list of all files in directory tree at given path listOfFiles = list() for (dirpath, dirnames, filenames) in os.walk(dirName): listOfFiles += [os.path.join(dirpath, file) for file in filenames] # Print the files for elem in listOfFiles: print(elem) if __name__ == '__main__': main()
Output:
/home/varun/Downloads/temp1.txt /home/varun/Downloads/sample/temp2.txt /home/varun/Downloads/test/message.txt
wow, worked perfectly. Thanks.
Thanks varnum, worked perfectly on my old (Mavericks) operating sys.