How to Iterate Over Files in Directory Using Python
# Iterating over Files in a Directory with Python
The ability to iterate over files in a directory can come in very handy when performing tasks such as gathering information on all the files in a directory, or performing an action on each file in the directory. Python provides a convenient way to quickly and easily iterate over files in a directory by using the os module. In this article, we will learn how to use the os module to iterate over files in a directory in Python.
The os module in Python provides a number of useful functions that allow us to interact with the operating system. This includes functions for listing files in a directory, creating new directories, and deleting files or directories.
In this article, we will focus on using the os module to iterate over files in a directory. We will cover two methods for iterating over files in a directory, using os.listdir and os.scandir. Both methods will return a list of filenames, which can then be used to perform further actions on each file.
We will also look at how to use the os.walk method, which can be used to iterate over subdirectories within a directory. This is useful for tasks such as searching through all subdirectories of a directory to find a particular file.
Finally, we will look at how to filter the list of files that is returned by the os module, in order to only include files of a certain type, or that match a particular pattern. This can be useful for tasks such as finding all the images in a directory, or all the text files in a directory.
Worried About Failing Tech Interviews?
Attend our webinar on
"How to nail your next tech interview" and learn
.png)
Hosted By
Ryan Valles
Founder, Interview Kickstart

Our tried & tested strategy for cracking interviews

How FAANG hiring process works

The 4 areas you must prepare for

How you can accelerate your learnings
Register for Webinar
# Iterating Over Files in a Directory Using Python
Python provides a number of ways to access and iterate over files in a directory. Here are a few examples of how to do so:
## Using os.listdir()
The `os.listdir()` function returns a list of all the files and folders in a given directory.
```python
import os
# path to the directory
dir_path = '/path/to/directory'
# get list of files in the directory
file_list = os.listdir(dir_path)
# iterate over the list of files
for file in file_list:
print(file)
```
## Using glob
The `glob` module provides an easy way to access files in a directory. The `glob.glob()` function returns a list of all the files and folders in a directory that match a specified pattern.
```python
import glob
# path to the directory
dir_path = '/path/to/directory'
# pattern to match
pattern = '*.txt'
# get list of files in the directory
file_list = glob.glob(os.path.join(dir_path, pattern))
# iterate over the list of files
for file in file_list:
print(file)
```
## Using os.walk()
The `os.walk()` function is another way to iterate over files in a directory. It iterates over the directory tree recursively, yielding a 3-tuple for each directory it encounters. The 3-tuple contains the directory path, a list of subdirectories within that directory, and a list of files in that directory.
```python
import os
# path to the directory
dir_path = '/path/to/directory'
for root, dirs, files in os.walk(dir_path):
# iterate over the list of files
for file in files:
print(os.path.join(root, file))
```