- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read multiple text files from a folder in Python?(Tkinter)
Python is capable of handling files, objects, and creating different applications. We can use Python's extensions and packages to build and develop fully featured applications.
Suppose you want to control the files in your system; then Python provides an OS Module which has system-enabled functionalities to allow you interact with the files in the operating system.
Let us see how we can read multiple text files from a folder using the OS module in Python.
Import the OS module in your notebook.
Define a path where the text files are located in your system.
Create a list of files and iterate over to find if they all are having the correct extension or not.
Read the files using the defined function in the module.
Example
# Import the required libraries import os # Define the location of the directory path =r"C:/Users/Sairam/Documents/" # Change the directory os.chdir(path) def read_files(file_path): with open(file_path, 'r') as file: print(file.read()) # Iterate over all the files in the directory for file in os.listdir(): if file.endswith('.txt'): # Create the filepath of particular file file_path =f"{path}/{file}" read_files(file_path)
Output
Sample 1 ======== Welcome to Tutorialspoint. You are browsing the best resource for Online Education. Sample 2 ======== A distributed ledger is a type of data structure which resides across multiple computer devices, generally spread across locations or regions. Distributed ledger technology (DLT) includes blockchain technologies and smart contracts. While distributed ledgers existed prior to Bitcoin, the Bitcoin blockchain marks the convergence of a host of technologies, including timestamping of transactions, Peer-to-Peer (P2P) networks, cryptography, and shared computational power, along with a new consensus algorithm.
We have two text files in the specified location and the program read the contents of these two files and displayed the text on the console.
- Related Articles
- Python - Read all CSV files in a folder in Pandas?
- Ask a user to select a folder to read the files in Python
- How to read text files using LINECACHE in Python
- How to read all files in a folder to a single file using Java?
- How to copy files from one folder to another using Python?
- How to upload multiple files and store them in a folder with PHP?
- Python - How to Merge all excel files in a folder
- How to copy certain files from one folder to another using Python?
- How to make the Tkinter text widget read only?
- How to read a text file in Python?
- How to plot data from multiple two-column text files with legends in Matplotlib?
- How to get list of all files/folders from a folder in Java?
- How to read an entire line from a text file using Python?
- How to display multiple lines of text in Tkinter Label?
- How to read a number of characters from a text file using Python?
