How To Use Python Loop Through Files In Directory?

python loop through files in directory

How can we use Python loop through files in directory? Is it as easy as other languages ​​like Java, PHP, and JavaScript? We will answer those questions now.

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.

Using os.listdir() function.

Syntax of 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.

Directory structure:

python loop through files in directory

Code:

# 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)

Output:

app.py
bookmark.txt
learnshareit.xls
note.txt
text.doc

Using os.scandir() function.

os.scandir() function is used to get file information in Python.

Syntax:

os.scandir(path)

Parameter

  • 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

Output:

<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

Using os.walk() function.

The walk() function scans a directory to create an object containing the files or subdirectories in that directory.

Syntax:

os.walk(top, top-down, onerror, followlinks)

Parameters:

  • 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 os.walk().

# 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

Output:

['app.py', 'bookmark.txt', 'learnshareit.xls', 'note.txt', 'text.doc']

Summary

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:

Leave a Reply

Your email address will not be published. Required fields are marked *