Python - How To Read Text File
Submitted by razormist on Tuesday, August 7, 2018 - 19:38.
In this tutorial we will create a How To Read Text File Using Python. Python has a design philosophy which emphasizes code readability. Python is very easy to learn the syntax emphasizes readability and it can reduces time consuming in developing. You can use this to convert the units value. So let's now do the coding...
There you have it we just created a How To Read Text File Using Python. I hope that this simple tutorial help you for what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!
Getting started:
First you will have to download & install the Python IDLE's, here's the link for the Integrated Development And Learning Environment for Python https://www.python.org/downloads/.Importing Modules
After setting up the installation and the database, run the IDLE and click file and then new file. After that a new window will appear containing a black file this will be the text editor for the python. Then copy code that I provided below and paste it inside the IDLE text editor.- from tkinter import *
Setting up the Main Frame
After importing the modules, we will now then create the main frame for the application. To do that just copy the code below and paste it inside the IDLE text editor.- #==================================MAIN FRAME=====================================
- root = Tk()
- root.title("Python - Check File Exists")
- 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)
Assigning Variables
This is where the we will assign the variables. This code will assign each of the variables to be use later in the application.- #=================================VARIABLES=======================================
- FILE = StringVar()
Designing the Layout
After creating the Main Frame we will now add some layout to the application. Just kindly copy the code below and paste it inside the IDLE text editor.- #=================================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=('calibri', 16))
- lbl_title.pack()
- lbl_txt = Label(Left, text="Enter a text file", font=('calibri', 15))
- lbl_txt.pack()
- lbl_txt2 = Label(Bottom, text="Result", font=('calibri', 15))
- lbl_txt2.pack()
- lbl_status = Label(Bottom,font=('calibri', 12))
- lbl_status.pack()
- #=================================ENTRY WIDGETS===================================
- txt_file = Entry(Right, font=('calibri', 15), textvariable=FILE)
- txt_file.pack()
- #=================================BUTTON WIDGETS==================================
- btn_check = Button(BelowMid, text="Read File", command=Read_File)
- btn_check.pack(pady=10)
Creating the Main Function
This is where the code that contains the main functions. This code will read a text file when clicked. To do that just copy and write these blocks of code.- #=================================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()
Initializing the Application
After finishing the function save the application as index.py. This function will run the code and check if the main is initialize properly. To do that copy the code below and paste it inside the IDLE text editor.- #=================================INITIALIZATION==================================
- if __name__ == '__main__':
- root.mainloop()
Add new comment
- 243 views