Open All the Files in a Directory in Python
-
Open All Files in a Folder/Dictionary Using
os.walk()
in Python -
Open All the Files in a Folder/Directory With the
os.listdir()
Function in Python -
Open All the Files in a Folder/Directory With the
glob.glob()
Function in Python

You can mainly use three methods to open all files inside a directory in Python: the os.listdir()
function, os.walk()
function and the glob.glob()
function. This tutorial will introduce the methods to open all the files in a directory in Python. We’ve also included program examples you can follow.
Open All Files in a Folder/Dictionary Using os.walk()
in Python
Various OS modules in Python programming allow multiple methods to interact with the file system. It has a walk()
function that will enable us to list all the files in a specific path by traversing the directory either bottom-up or top-down and returning three tuples - root, dir, and files.
Syntax:
os.walk(r'pathname')
In the above syntax, r
is to read the root folder or directory, and the parameter pathname
is the path of the folder.
Example:
import os
for root, dirs, files in os.walk(r'/content/drive/MyDrive/Skin Cancer'):
for file in files:
if file.endswith('.zip'):
print(os.path.join(root, file))
In the code, we first imported the OS module. Then in the read mode, we used a for
loop and passed the pathname to the walk function.
The loop iterates through all files that meet the file extension condition. The above code will read all files with a .zip
extension.
Output:
/content/drive/MyDrive/Skin Cancer/archive.zip
As you can see, the Google drive Skin Cancer
folder contains one zip file.
Open All the Files in a Folder/Directory With the os.listdir()
Function in Python
The listdir()
function inside the os
module is used to list all the files inside a specified directory. This function takes the specified directory path as an input parameter and returns the names of all the files inside that directory. We can iterate through all the files inside a specific directory using the os.listdir()
function and open them with the open()
function in Python.
The following code example shows us how we can open all the files in a directory with the os.listdir()
and open()
functions.
import os
for filename in os.listdir("files"):
with open(os.path.join("files", filename), 'r') as f:
text = f.read()
print(text)
Output:
This is the first file.
This is the second file.
This is the last file.
We read the text from the three files inside the files/
directory and printed it on the terminal in the code above. We first used a for/in
loop with the os.listdir()
function to iterate through each file found inside the files
directory. We then opened each file in read
mode with the open()
function and printed the text inside each file.
Open All the Files in a Folder/Directory With the glob.glob()
Function in Python
The glob
module is used for listing files inside a specific directory. The glob()
function inside the glob
module is used to get a list of files or subdirectories matching a specified pattern inside a specified directory. The glob.glob()
function takes the pattern as an input parameter and returns a list of files and subdirectories inside the specified directory.
We can iterate through all the text files inside a specific directory using the glob.glob()
function and open them with the open()
function in Python. The following code example shows us how we can open all files in a directory with the glob.glob()
and open()
functions:
import glob
import os
for filename in glob.glob('files\*.txt'):
with open(os.path.join(os.getcwd(), filename), 'r') as f:
text = f.read()
print(text)
Output:
This is the first file.
This is the second file.
This is the last file.
We read the text from the three files inside the files/
directory and printed it on the terminal in the code above. We first used a for/in
loop with the glob.glob()
function to iterate through each file found inside the files
directory. We then opened each file in read
mode with the open()
function and printed the text inside each file.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedIn