![]() |
VOOZH | about |
Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requirements. In this article, we will see how we can list all files in a directory and subdirectories in Python.
Below are some of the ways by which we can list all files in directory and subdirectories in Python:
In this example, the Python function `list_files_recursive` recursively traverses a specified directory (`'./'` by default), printing the full paths of all files within that directory and its subdirectories. The function uses the `os` module to list and navigate directories.
Output:
In this example, the Python function `list_files_walk` recursively traverses a specified directory (`'./'` by default) using `os.walk`, printing the full paths of all files within that directory and its subdirectories. The function takes advantage of the `os` module's `walk` function, which efficiently generates file paths during directory traversal.
Output:
👁 ezgifcom-crop-(1)In this example, the Python function `list_files_glob` uses the `glob` module to list and print the full paths of files matching a specified pattern (`'./**/*'` by default). The `recursive` parameter is set to `True` by default, allowing it to recursively search subdirectories. The function demonstrates a concise way to obtain file paths using globbing patterns.
Output:
👁 ezgifcom-crop-(1)In this example, the Python function `list_files_scandir` utilizes the `os.scandir` method to efficiently iterate over entries (files and directories) in a specified directory (`'./'` by default). It prints the full paths of files and recursively explores subdirectories using `os.scandir`'s context manager, enhancing performance compared to using `os.listdir` for large directories.
Output:
👁 ezgifcom-crop-(1)In this example, the Python function `list_files_pathlib` utilizes the `pathlib` module to list and print the paths of files and directories in a specified directory (`'./'` by default). It uses the `iterdir` method to iterate over entries, and if the entry is a file, its path is printed; if it's a directory, the function recursively explores its contents. The use of `pathlib` provides a more object-oriented and readable way to work with file paths.
Output:
👁 ezgifcom-crop-(1)When it comes to choosing the best approach for listing files in a directory and its subdirectories, consider the following best practices:
In conclusion, Python offers developers a diverse set of tools to tackle the task of listing files in a directory and its subdirectories. The choice of approach depends on factors such as code readability, Python version compatibility, and specific use case requirements. Whether you opt for the simplicity of os.walk, the flexibility of glob, the efficiency of os.scandir, or the modernity of pathlib, each method empowers you to handle this common task with confidence. By understanding these approaches, you can enhance your coding skills and choose the best tool for the job at hand.