Python - How To Write To File

In this tutorial we will create on How To Write To File using Python. Python is a widely used advance-level programming language for a general technique to the developer. Beginners find Python a clean syntax and indentation structure based, and it is easy to learn because of its less semi colon problem. So let do the 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.
  1. 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.
  1. #==================================MAIN FRAME=========================================
  2. root = Tk()
  3. root.title("How To Write To File")
  4. width=500
  5. height=400
  6. screen_width=root.winfo_screenwidth()
  7. screen_height=root.winfo_screenheight()
  8. x=(screen_width/2) - (width/2)
  9. y=(screen_height/2) - (height/2)
  10. root.geometry("%dx%d+%d+%d" % (width, height, x, y))
  11. 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.
  1. #=================================VARIABLES===========================================
  2. TITLE = StringVar()
  3. CONTENT = 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.
  1. #=================================FRAMES==============================================
  2. Top = Frame(root, bd=1, relief=SOLID,)
  3. Top.pack(side=TOP, fill=X)
  4. Mid = Frame(root)
  5. Mid.pack(pady=20)
  6. LeftPane = Frame(Mid)
  7. LeftPane.pack(side=LEFT)
  8. RightPane = Frame(Mid)
  9. RightPane.pack(side=RIGHT)
  10. Bottom = Frame(root)
  11. Bottom.pack(side=TOP)
  12. BottomPane = Frame(root)
  13. BottomPane.pack(side=TOP)
  14.  
  15. #=================================LABEL WIDGETS=======================================
  16. lbl_text = Label(Top, text="Writing A File", font=('arial', 20))
  17. lbl_text.pack()
  18. lbl_title = Label(LeftPane, text="Title", font=('arial', 18))
  19. lbl_title.pack()
  20. lbl_content = Label(Bottom, text="Content", font=('arial', 18))
  21. lbl_content.pack(side=TOP)
  22. lbl_note = Label(RightPane, text=".txt", font=('arial', 18))
  23. lbl_note.pack(side=RIGHT)
  24. lbl_status = Label(Bottom, font=('times new roman', 12))
  25. lbl_status.pack(side=BOTTOM)
  26. #=================================ENTRY WIDGETS========================================
  27. txt_title = Entry(RightPane, font=('arial', 18), width=15, textvariable=TITLE)
  28. txt_title.pack()
  29. txt_content = Text(Bottom, font=('arial', 18), width=15, height=6)
  30. txt_content.pack(side=TOP)
  31.  
  32.  
  33. #=================================BUTTON WIDGETS======================================
  34. btn_save = Button(BottomPane, text="Save a file", command=SaveFile)
  35. btn_save.pack(pady=5)

Creating the Main Function

This is where the code that contains the main funcitions. This code will create a text file entitle base on the users inputs, then can write a text on the newly created text file. To do that just copy and write these blocks of code.
  1. #=================================METHODS=============================================
  2. def SaveFile():
  3.     CONTENT = txt_content.get("1.0", "end-1c")
  4.     if TITLE.get() == "" and CONTENT == "":
  5.         lbl_status.config(text="Please fill the field first!", fg="orange")
  6.     else:
  7.         file = open(TITLE.get() + ".txt", "w")
  8.         file.write(CONTENT)
  9.         TITLE.set('')
  10.         txt_content.delete('1.0', END)
  11.         file.close()
  12.         lbl_status.config(text="Successfully created a file!", fg="Green")

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.
  1. #=================================INITIALIZATION======================================
  2. if __name__ == '__main__':
  3.     root.mainloop()
There you have it we just created on How To Write To 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!!

Add new comment