List Every File in a Directory
It's simple as pie to get a directory's file list! To list all files in a directory, use an OS module's listdir() and isfile() functions. These are the procedures.
1) Add the OS module.
With the aid of this module, we can use Python's operating system-specific features. The OS module provides functions for dealing with the operating system.
2) Use the procedure os.listdir()
The list of files and directories in the directory indicated by the path is returned by the os.listdir('path') function.
3) Refine the outcome.
The listdir() function's returned files can be iterated using a for a loop. We will traverse each file given by the listdir() function using a loop.
4) Employ the isfile() method.
Use the os. path.isfile('path') function to determine whether the current approach is a file or directory for each loop iteration. Add it to a list if it's a file. If a path is a file, this function returns True. Otherwise, False is returned.
Example of a Directory File List
Let's practice listing the files in the "account" folder. Only files in the current directory will be recorded by listdir(); subdirectories will not be considered.
Example 1
List only the files in a directory
We have three file names here.
If you are familiar with generator expressions, you can use a generator function to create smaller, simpler code, as demonstrated below.
Generator Expression
After that, just call it as needed.
Example 2
List both files and directories
To access the contents of a directory, use the listdir('path') method directly.
Output:
The output shows that "reports 2021" is a directory.
os.walk() to list all files in directory and subdirectories
A generator that creates a tuple of values (current path, directories in current path, and files in current path) is returned by the os.walk() function.
Note: We may list all directories, subdirectories, and files in one directory by using the os. walk() function. Every time the generator is used, it will follow each directory recursively to get a list of files and directories until no more sub-directories are accessible from the initial directory because it is a recursive function.
For each directory that the os. Walk ('path') command traverses; two lists will be returned. Files are included in the first list, and directories are included in the second list.
Let's look at an example of how to list every file in a directory and its subdirectories.
Example:
Output:
Using os.scandir() to retrieve files from a directory
For many typical use scenarios, the scandir() function returns directory entries along with file attribute information and offers improved speed.
It gives back an iterator of file name-containing os.DirEntry objects.
Example:
Output:
Files in a Directory are listed by the Glob Module
To locate files and folders whose names match a given pattern, use the Python glob module from the Python Standard Library.
For instance, we will use the dire path/*.* pattern to retrieve all files in a directory. Any file with the extension *.* is meant here.
Example:
Output:
Note: Set the recursive attribute to True if you want to list files from subdirectories.
Example:
Output:
To list the files in a directory using Pathlib Module
We can utilize the pathlib module, which offers a wrapper for most OS functions, starting with Python 3.4.
1) Import the pathlib module: For many operating systems, the pathlib module provides classes and methods to manage filesystem paths and retrieve information about files.
2) Use the pathlib next.
Directory path construction using Path('path')
3) After that, use iterdir() to loop through each entry in the directory.
4) Finally, use the path.isfile() function to determine whether the current element is a file.
Example: