In this article, I will tell you several methods about how to use python os
, pathlib
module to list files in directory. It will also tell you how to use the python fnmatch
module to filter out special files that match the file name pattern.
1. Use Python OS Module Listdir Method.
The python os module’s listdir
method returns all the files and directories in the given path. The below example will list all the files and subfolder files in the provided folder path.
import os path = '/Users/songzhao/Documents/Tool' def print_files_in_folder(path): # Get files list in the directory. file_list = os.listdir(path) # Loop in above files list. for f in file_list: # Get the file full path. f_path = os.path.join(path, f) # If the file is a file. if(os.path.isfile(f_path)): # Get the file absolute path. f_path_str = os.path.join(path,f) # Print the file absolute path. print(f_path_str) else: # If the file is a directory, then call the method recursively. print_files_in_folder(f_path) if __name__ == '__main__': print_files_in_folder(path)
Below is the output when executing the above python code.
/Users/songzhao/Documents/Tool/.DS_Store /Users/songzhao/Documents/Tool/test/AppDelegate.swift /Users/songzhao/Documents/Tool/Book1.pdf
2. Use Python OS Module Walk Method.
The python os module’s walk
method will iterate all the files and folders in the current directory recursively.
import os from docutils.utils.math.math2html import file path = '/Users/songzhao/Documents/Tool' def print_files_in_folder_os_walk(path): result = os.walk(path) for root, dirs, files in result: for f in files: f_path = root+'/'+f print(f_path) if __name__ == '__main__': print_files_in_folder_os_walk(path)
Below is the output when executing the above python code.
/Users/songzhao/Documents/Tool/.DS_Store /Users/songzhao/Documents/Tool/Book1.pdf /Users/songzhao/Documents/Tool/test/AppDelegate.swift
3. Use Python Pathlib Path Class.
import pathlib def print_files_in_folder_path_lib(path): # Define the pathlib.Path object. p_dir = pathlib.Path(path) # Loop all the files in above Path object. for f in p_dir.iterdir(): # Define the pathlib.Path object based on the file object. f_obj = pathlib.Path(f) # If it is a file, then print it's path. if f_obj.is_file(): print(f) else: # If it is a directory, then call this method again. print_files_in_folder_path_lib(f) if __name__ == '__main__': print_files_in_folder_path_lib(path)
Below is the output when executing the above python code.
/Users/songzhao/Documents/Tool/.DS_Store /Users/songzhao/Documents/Tool/test/AppDelegate.swift /Users/songzhao/Documents/Tool/Book1.pdf
4. Use Python Fnmatch Module To Filter List Files.
The python fnmatch
module can be used to filter out special files that match the file name pattern, below example code will filter out all the pdf files under provided directory.
import os import fnmatch def print_files_in_folder_os_fnmatch(path): # List all the files under the path. files = os.listdir(path) # Create file name match pattern. f_pattern = "*.pdf" # Loop in the files list. for f in files: # Get the file full path. f_path = os.path.join(path, f) # If the file is a file. if(os.path.isfile(f_path)): # Call fnmatch module's fnmatch method to check whether the file name match the pattern or not. match = fnmatch.fnmatch(f, f_pattern) # If the file name match the file name pattern. if match: # Get the file object absolute path. f_path_str = os.path.join(path,f) # Print the file path. print(f_path_str) else: # If the file is a directory, then reverse call the method. print_files_in_folder_os_fnmatch(f_path) if __name__ == '__main__': print_files_in_folder_os_fnmatch(path)
Below is the output when executing the above python code.
/Users/songzhao/Documents/Tool/Book1.pdf