How To Read Data From Text File In Python With Source Code
The Reading Text File In Python is one of the most common tasks that you can do with Python, In this tutorial i will teach you on How To Read A Text File In Python.
A simple tutorial Read Text File In Python is mainly for beginner to intermediate Pythonistas, but there are some tips in here that more advanced programmers may appreciate as well. This tutorial also includes the downloadable Read Data From Text File In Python with source code for free.
Watch the video here on How to Read Data From text file in python.
To start creating this simple tutorial on How To Read Data From Text File In Python, make sure that you have PyCharm IDE installed in your computer.
Steps on How To Read Data From Text File In Python With Source Code
How To Read Data From Text File In Python With Source Code
- Step 1: Create a project name.
First open Pycharm IDE and then create a “project name” after creating a project name click the “create” button.
- Step 2: Create a python file.
Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.
- Step 3: Name your python file.
Third after creating a python file, Name your python file after that click “enter“.
- Step 4: The actual code.
You are free to copy the code given below and download the full source code below.
The code given below is for importing module.
1 |
from tkinter import * |
This module which importing the tkinter for the Graphical User Interface (GUI).
The code given below is for the design.
1 2 3 4 5 6 7 8 9 10 |
root = Tk() root.title("Read Text File In Python") width=450 height=350 screen_width=root.winfo_screenwidth() screen_height=root.winfo_screenheight() x=(screen_width/2) - (width/2) y=(screen_height/2) - (height/2) root.geometry("%dx%d+%d+%d" % (width, height, x, y)) root.resizable(0, 0) |
This module which is the design of this project.
The code given below is for the declaring of the variable.
1 |
FILE = StringVar() |
This module which is declaring of the variable FILE to be call in public.
The code given below is for reading the data in text file
1 2 3 4 5 6 7 8 9 10 11 12 13 |
def Read_File(): if FILE.get() == "": lbl_status.config(fg="Orange", text="Please enter something") for widget in BottomPane.winfo_children(): widget.destroy() else: for widget in BottomPane.winfo_children(): widget.destroy() file = open(FILE.get() + ".txt", "r") chars = [line.rstrip('\n') for line in file] for i in range(len(chars)): exec ('Label%d=Label(BottomPane,text="%s")\nLabel%d.pack(anchor=W)' % (i, chars[i], i)) file.close() |
This module which is reading of the data in a text file and display into the tkinter design.
Complete Source Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
from tkinter import * #==================================MAIN FRAME===================================== root = Tk() root.title("Read Text File In Python") width=450 height=350 screen_width=root.winfo_screenwidth() screen_height=root.winfo_screenheight() x=(screen_width/2) - (width/2) y=(screen_height/2) - (height/2) root.geometry("%dx%d+%d+%d" % (width, height, x, y)) root.resizable(0, 0) #=================================VARIABLES======================================= FILE = StringVar() #=================================METHODS========================================= def Read_File(): if FILE.get() == "": lbl_status.config(fg="Orange", text="Please enter something") for widget in BottomPane.winfo_children(): widget.destroy() else: for widget in BottomPane.winfo_children(): widget.destroy() file = open(FILE.get() + ".txt", "r") chars = [line.rstrip('\n') for line in file] for i in range(len(chars)): exec ('Label%d=Label(BottomPane,text="%s")\nLabel%d.pack(anchor=W)' % (i, chars[i], i)) file.close() #=================================FRAMES========================================== Top = Frame(root, relief=SOLID, bd=1) Top.pack(fill=X) Mid = Frame(root) Mid.pack(pady=20) Left = Frame(Mid) Left.pack(side=LEFT) Right = Frame(Mid) Right.pack(side=RIGHT) BelowMid = Frame(root) BelowMid.pack() Bottom = Frame(root) Bottom.pack() BottomPane = Frame(root) BottomPane.pack() #=================================LABEL WIDGETS=================================== lbl_title = Label(Top, text="Read File Text", font=('Courier new', 16)) lbl_title.pack() lbl_txt = Label(Left, text="Enter a text file", font=('Courier new', 15)) lbl_txt.pack() lbl_txt2 = Label(Bottom, text="Result", font=('Courier new', 15)) lbl_txt2.pack() lbl_status = Label(Bottom,font=('Courier new', 12)) lbl_status.pack() #=================================ENTRY WIDGETS=================================== txt_file = Entry(Right, font=('Courier new', 15),fg='blue', textvariable=FILE) txt_file.pack() #=================================BUTTON WIDGETS================================== btn_check = Button(BelowMid, text="Read File",bg='green', command=Read_File) btn_check.pack(pady=10) #=================================INITIALIZATION================================== if __name__ == '__main__': root.mainloop() |
Output

Question
Before we can go into how to work with files in Python, it’s important to understand what exactly a file is and how modern operating systems handle some of their aspects.
At its core, a file is a contiguous set of bytes used to store data. This data is organized in a specific format and can be anything as simple as a text file or as complicated as a program executable. In the end, these byte files are then translated into binary 1
and 0
for easier processing by the computer.
Run Quick Virus Scan for secure Download
Run Quick Scan for secure DownloadDownloadable Source Code
I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.
Summary
The Reading Text File In Python is one of the most common tasks that you can do with Python, This tutorial is written in python programming language. Python is very smooth to research the syntax emphasizes readability and it is able to reduces time ingesting in developing.Also in this tutorial is the simplest way for the beginners or the student to enhance their logical skills in programming.
Project file contains the python script(index.py) and the text file (file.txt).
Related Articles
- Best Python Project with Source code free to download
- Hotel Management System Project in Python With Source Code
- Student Management System Project in Python with Source Code
- How To Make A Point Of Sale System In Python
- Best Python Projects for Beginners
- Python MySQL Connection: Simple Python Step by Step Guide
- Python PIP Command Set-up / Fix: Step by Step Guide
- Random Password Generator in Python Projects With Source Code 2020
- Python Range Function|Range in Python Explained with Examples 2020
- Billing System Project in Python Project With Source Code
- Employee Payment Management System Project in Python
- School Management System Project In Python With Source Code
- Bank Management System Project in Python With Source Code
Inquiries
If you have any questions or suggestions about on How To Read Data From Text File In Python, please feel free to leave a comment below.