How do i get a list of files in a directory and subfolders in python?
Pretty simple solution would be to run a couple of sub process calls to export the files into CSV format: Show
That command produces comma separated values that can be easily analyzed in Excel.
The resulting CSV file doesn't have a header row, but you can use a second command to add them.
Depending on how much data you get back, you can massage it further using Pandas. Here are some things I found useful, especially if you're dealing with many levels of directories to look through. Add these to your imports:
Then add this to your code: In this article we will discuss different methods to generate a list of all files in directory tree. Python’s os module provides a function to get the list of files or folder in a directory i.e. 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. ''' 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,
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, 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 Advertisements How do I get a list of files in a directory and subdirectories?Here are the steps to get a list of all the file names from a folder:. Go to the Data tab.. In the Get & Transform group, click on New Query.. Hover the cursor on the 'From File' option and click on 'From Folder'.. In the Folder dialog box, enter the folder path, or use the browse button to locate it.. Click OK.. How do you read all files in a directory and subfolders in Python?os. listdir() method in python is used to get the list of all files and directories in the specified directory.
How do you get a list of all files in a directory in Python?To get a list of all the files and folders in a particular directory in the filesystem, use os. listdir() in legacy versions of Python or os. scandir() in Python 3.
|