Create a Simple Text Editor with Python

Learn to code your own text editor with Python under 5 minutes

File Handling

Opening and Closing a file

  • "r" - Read - Default value. Opens a file for reading. Produces an error if the file doesn't exist.
  • "a" - Append - Opens a file for appending, creates the file if it does not exist.
  • "w" - Write - Opens a file for writing, creates the file if it does not exist.
  • "x" - Create - Creates the specified file, returns an error if the file exists
  • "t" - Text - Default value. Text mode.
  • "b" - Binary - Binary mode (e.g. images).
file = open("path/to/file.txt", "rb")
file = open("file.txt", "w")    # Opens a file in write mode
file.close() # Closes the file

Reading a File

Writing to files

  • "a" - Append - will append to the end of the file
  • "w" - Write - will overwrite any existing content
from os import path
file_path = input("\nCreate file (please enter the path to file): ")
print("\nPress RETURN to start a new line.\nPress Ctrl + C to save and close.\n\n")

Output

Resources

  • Python conditional statements 👉 visit
  • Python while loops 👉 visit
  • Python file handling 👉 visit
  • Python os module 👉 visit
  • Python try/except 👉 visit

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shounak Das

I am a passionate programmer and tech-geek. I love to code, and teach coding to beginners.