Importing Multiple Files in Python
Last Updated :
26 Mar, 2024
Here, we have a task to import multiple files from a folder in Python and print the result. In this article, we will see how to import multiple files from a folder in Python using different methods.
Import Multiple Files From a Folder in Python
Below, are the methods of importing multiple files from a folder in Python.
- Using os and open
- Using glob Method
- Using pandas Method
File structure :

index.txt : GeeksforGeeks
index.csv : GeeksforGeeks
index.tsv : GeeksforGeeks
index.html : <h1>GeeksforGeeks</h1>
Import Multiple Files From A Folder using OS and Open
In this code example, below code utilizes the `os` module to list files in the "templates" folder. It then iterates through the files, reads those with a ".txt" extension, and prints their content. The script uses the `open` function to access each file, reads its content.
Python3
import os
folder_path = "templates"
files = os.listdir(folder_path)
for file in files:
if file.endswith(".txt"):
file_path = os.path.join(folder_path, file)
with open(file_path, 'r') as f:
data = f.read()
# Process the data as needed
print(f"File: {file}\nData:\n{data}\n{'=' * 30}")
Output
File: index.txt
Data:
GeeksforGeeks
==============================
Import Multiple Files From A Folder using glob Method
In this example, below uses the glob module to create a list of CSV files in the "templates" folder. It then iterates through the list, opens each file, reads its content, and prints file information along with the data. The script employs the os.path.join function to construct the file path and displays a separator line after each file's data.
Python3
import glob
import os
folder_path = "templates"
csv_files = glob.glob(os.path.join(folder_path, '*.csv'))
for file in csv_files:
with open(file, 'r') as f:
data = f.read()
# Process the CSV data as needed
print(f"File: {file}\nData:\n{data}\n{'=' * 30}")
Output
File: templates\index.csv
Data:
GeeksforGeeks
==============================
Import Multiple Files From A Folder using pandas Method
In this example, below Python code the `Pandas` library to read multiple HTML files from the "templates" folder. It uses `glob` to create a list of HTML files, reads each file into a Pandas DataFrame, and concatenates them into a single DataFrame named `combined_data`. The final DataFrame can be further processed as needed, and its contents are printed.
Python3
import pandas as pd
import glob
import os
folder_path = "templates"
csv_files = glob.glob(os.path.join(folder_path, '*.html'))
dfs = [pd.read_csv(file) for file in csv_files]
combined_data = pd.concat(dfs, ignore_index=True)
# Process the combined_data dataframe as needed
print(combined_data)
Output
Empty DataFrame
Columns: [<h1>GeeksforGeeks</h1>]
Index: []
Similar Reads
Import module in Python In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
fileinput.input() in Python With the help of fileinput.input() method, we can get the file as input and can be used to update and append the data in the file by using fileinput.input() method. Python fileinput.input() Syntax Syntax : fileinput.input(files) Parameter : fileinput module in Python has input() for reading from mul
2 min read
Create and Import modules in Python In Python, a module is a self-contained Python file that contains Python statements and definitions, like a file named GFG.py, can be considered as a module named GFG which can be imported with the help of import statement. However, one might get confused about the difference between modules and pac
3 min read
Importlib package in Python In this article, we are going to know about the Importlib package in the Python programming language. The importlib package is primarily utilized by Python applications for dynamic imports during runtime. In layman's terms, it allows the user to load modules as he/she discovers them. This package ha
6 min read
Lazy import in Python In this article, we will learn how to do Lazy import in Python. For this, let's first understand the lazy import. What is Lazy Import? It is a feature of the Pyforest module that allows the user to perform the task without adding libraries to the code snippet as the task of this function is to add t
5 min read
Python - Import module outside directory Modules are simply a python .py file from which we can use functions, classes, variables in another file. To use these things in another file we need to first import that module into that file. If the module exists in the same directory as the file, we can directly import it using the syntax import
4 min read
Import Modules From Another Folder in Python In this article, we are going to see how to import a module from another folder, While working on big projects we may confront a situation where we want to import a module from a different directory, here we will see the different ways to import a module form different folder. It can be done in two
2 min read
File Mode in Python In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho
5 min read
Interact with files in Python Python too supports file handling and allows users to handle files i.e., to read, write, create, delete and move files, along with many other file handling options, to operate on files. The concept of file handling has stretched over various other languages, but the implementation is either complica
6 min read
File Handling in Python File handling refers to the process of performing operations on a file such as creating, opening, reading, writing and closing it, through a programming interface. It involves managing the data flow between the program and the file system on the storage device, ensuring that data is handled safely a
7 min read