Open In App

How to read multiple text files from folder in Python?

Last Updated : 02 Feb, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Prerequisite:

Python is a strong language which is extremely capable even when it comes to file handling. In this article, we will learn how to read multiple text files from a folder using python.

Approach:

  • Import modules
  • Add path of the folder
  • Change directory
  • Get the list of a file from a folder
  • Iterate through the file list and check whether the extension of the file is in .txt format or not.
  • If text-file exist, read the file using File Handling

Functions used:

  • os.chdir() method in Python used to change the current working directory to specified path. It takes only a single argument as new directory path.

Syntax: os.chdir(path)

Parameters:

  • path: A complete path of directory to be changed to new directory path.

Returns: Doesn’t return any value

  • os.listdir() method in python is used to get the list of all files and directories in the specified directory. If we don’t specify any directory, then list of files and directories in the current working directory will be returned.

Syntax: os.listdir(path)

Parameters:

  • path (optional) : path of the directory

Return Type: This method returns the list of all files and directories in the specified path. The return type of this method is list.

Below is the Implementation:

Program:

Python3




# Import Module
import os
  
# Folder Path
path = "Enter Folder Path"
  
# Change the directory
os.chdir(path)
  
# Read text File
  
  
def read_text_file(file_path):
    with open(file_path, 'r') as f:
        print(f.read())
  
  
# iterate through all file
for file in os.listdir():
    # Check whether file is in text format or not
    if file.endswith(".txt"):
        file_path = f"{path}\{file}"
  
        # call read text file function
        read_text_file(file_path)


Output:


Previous Article
Next Article

Similar Reads

How to read all CSV files in a folder in Pandas?
In this article, we will see how to read all CSV files in a folder into single Pandas dataframe. The task can be performed by first finding all CSV files in a particular folder using glob() method and then reading the file by using pandas.read_csv() method and then displaying the content. Approach:Import necessary python packages like pandas, glob,
1 min read
Read multiple CSV files into separate DataFrames in Python
Sometimes you might need to read multiple CSV files into separate Pandas DataFrames. Importing CSV files into DataFrames helps you work on the data using Python functionalities for data analysis. In this article, we will see how to read multiple CSV files into separate DataFrames. For reading only one CSV file, we can use pd.read_csv() function of
2 min read
How to read large text files in Python?
In this article, we will try to understand how to read a large text file using the fastest way, with less memory usage using Python. To read large text files in Python, we can use the file object as an iterator to iterate over the file and perform the required task. Since the iterator just iterates over the entire file and does not require any addi
3 min read
How to merge multiple excel files into a single files with Python ?
Normally, we're working with Excel files, and we surely have come across a scenario where we need to merge multiple Excel files into one. The traditional method has always been using a VBA code inside excel which does the job but is a multi-step process and is not so easy to understand. Another method is manually copying long Excel files into one w
4 min read
Python - Move all files from subfolders to main folder
This article will discuss how to move all files from the subfolder to the main folder using Python. The approach is simple it is similar to moving files from one folder to another using Python, except here the main folder or parent folder of the subfolder is passed as the destination. Modules UsedOS Module: OS module in Python provides functions to
3 min read
How to Merge all excel files in a folder using Python?
In this article, we will see how to combine all Excel files present in a folder into a single file. Module used: The python libraries used are: Pandas: Pandas is a python library developed for a python programming language for manipulating data and analyzing the data. It is widely used in Data Science and Data analytics.Glob: The glob module matche
3 min read
Python - Copy Files From Subfolders to the Main folder
In this article, we will discuss how to copy a file from the subfolder to the main folder. The directory tree that will be used for explanation in this article is as shown below: D:\projects\base | |__subfolder: | \__file.txt | |__main.py | Here we have a folder named "base" in which we have a folder named "subfolder" which contains a file name fil
4 min read
Remove all empty files within a folder and subfolders in Python
In this article, we will see how to remove all empty files within a folder and its subfolders in Python. We occasionally produce some empty files that are not needed. Here, we will use the below functions/methods to remove all empty files within a folder and its subfolder imported from the OS module and glob module. MethodDescriptionos.walk(path)Ge
4 min read
How to read multiple data files into Pandas?
In this article, we are going to see how to read multiple data files into pandas, data files are of multiple types, here are a few ways to read multiple files by using the pandas package in python. The demonstrative files can be download from here Method 1: Reading CSV files If our data files are in CSV format then the read_csv() method must be use
3 min read
How to Read Text Files with Pandas?
In this article, we will discuss how to read text files with pandas in Python. In Python, the Pandas module allows us to load DataFrames from external files and work on them. The dataset can be in different types of files. Text File Used Read Text Files with PandasBelow are the methods by which we can read text files with Pandas: Using read_csv()Us
4 min read
Practice Tags :