How to use Python loop through files in directory?
Here are 3 ways Python uses to iterate through files in a directory, read through, and compare them with other programming languages to see if working with files in Python is easier.
os.listdir() function we mentioned in this article. You can read more if you want.
Since this function uses the os module, we must import os at the beginning of the file. Then we pass a path to the directory to the
listdir() function so that we can use the For loop to iterate through all the files in that directory. See the example below to understand.
# Import os module import os # Specify the path to the directory directory = 'assets/document' # Loop through files in the directory for filename in os.listdir(directory): print(filename)
app.py bookmark.txt learnshareit.xls note.txt text.doc
os.scandir() function is used to get file information in Python.
- path: A path to the directory to get information.
First, we also import the os module. Then pass a path to
scandir() to iterate over. Unlike
listdir(), the element in each
scandir() loop is a DirEntry. We can get the name or path of a DirEntry object by calling the name or path property. Like this:
# Import os module import os # Specify the path to the directory directory = 'assets/document' # Loop through files in the directory for file in os.scandir(directory): print(file) # DirEntry object print('filename: ', file.name) # String
<DirEntry 'app.py'> filename: app.py <DirEntry 'bookmark.txt'> filename: bookmark.txt <DirEntry 'learnshareit.xls'> filename: learnshareit.xls <DirEntry 'note.txt'> filename: note.txt <DirEntry 'text.doc'> filename: text.doc
walk() function scans a directory to create an object containing the files or subdirectories in that directory.
os.walk(top, top-down, onerror, followlinks)
- top: Specify a directory to be scanned.
- top-down (optional): If True, directories are scanned from the top. If False, directories are scanned from the bottom up.
- onerror (optional): Display an error to continue the scan or raise an exception to stop the process.
- followlinks (optional): If true, the visit dir is pointed to by the symbolic link.
This way, we also import os and specify the path to the directory to be scanned like the above.
The difference is that when using the For loop, we have to initialize 3 variables: root, dirs, and files. The files variable is the variable that holds all the files in the specified directory. See the example below for how to use
# Import os module import os # Specify the path to the directory directory = 'assets/document' # Loop through files in the directory for root, dirs, files in os.walk(directory): print(files) # List of all files
['app.py', 'bookmark.txt', 'learnshareit.xls', 'note.txt', 'text.doc']
As you can see, just with the os module in Python, we can efficiently loop through and work with files in a directory. We hope this article was helpful to you.
Have a nice day!
Maybe you are interested:
Name of the university: HCMUE