Create a Simple Text Editor with Python

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

Shounak Das
5 min readDec 22, 2020

In this post, I will show you a very easy way to create a simple text editor with Python. This tutorial is meant for absolute beginners. If you know the fundamentals of Python and a little bit of file handling, you can make one yourself. Before I begin, I would be explaining a bit of file handling for those who don’t know anything about it.

File Handling

File handling is basically working with files using a programming language. You can open a file, read, write, delete data, and close it again, making permanent changes. Apart from these, there are many more operations, but we don’t need to learn all those for this project. I will leave a link to a reference at the end of this post. You can visit it to learn more.

Opening and Closing a file

To open a file, we use the open() function. The open() function takes two parameters - file path, and mode.

There are four modes to open 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

In addition, you can specify if the file should be handled as binary or text mode.

  • "t" - Text - Default value. Text mode.
  • "b" - Binary - Binary mode (e.g. images).

Example:-

file = open("path/to/file.txt", "rb")

Here, the mode "rb" tells python to "read the file in binary mode."

After you open a file and work on it, you need to close it. Python has the close() function that closes a file.

file = open("file.txt", "w")    # Opens a file in write mode
file.close() # Closes the file

Reading a File

The read() function allows us to read the contents of a file. It takes an optional argument (an integer) that specifies how many characters to read.

Writing to files

Two modes allow us to write to files:

  • "a" - Append - will append to the end of the file
  • "w" - Write - will overwrite any existing content

That’s enough file handling for now. Let’s move on to the main part.

First, create a new python file. We will be using the path module from os. So, let's import it.

from os import path

This will help us to check existing files. Now, we need the user to enter the path to the new or existing file. Let’s store it in a variable file_path.

file_path = input("\nCreate file (please enter the path to file): ")

Next, we need to check if the file already exists using the exists() function of the path module. For this, we need to write an if statement. The exists() function checks if the file already exists at the given path, in this case, file_path.

If the file does not exist, then it will create a new file at the same location. Otherwise, it will ask if we wish to continue with the existing file. So, we need another if statement.

If we wish to continue with the existing file, it will ask if we wish to delete the content, or do we want to append text to it. Otherwise, the program will stop. So, let’s write another if statement inside this one.

The pass keyword tells the compiler to just do nothing and move on to the next steps. Putting all parts together, your code should look something like this:

Now comes the most interesting part where we will build the interface. We need to inform the user how to use our text editor beforehand. So, paste this print statement.

print("\nPress RETURN to start a new line.\nPress Ctrl + C to save and close.\n\n")

And now, paste these lines:

As you know, most text editors put a line number at the start of every line. Here, we will be doing something like that. I have used an infinite while loop so that you can add as many lines as you want to. But, we can't let it run forever. So, I have used a try/ except statement, with the exception being raised by a KeyboardInterrupt, caused by pressing Ctrl + C.

If you don’t know what the try/ except statement is used for, in short words, it tries to run the code unless an exception (e.g. pressing a key, reaching the end of file, etc.) occurs.

Here, the line variable will store a single line of text, then it will write it to the file, add a new line, increment line_count by one, and again repeat unless you press Ctrl + C, which is used to stop a while loop (not to copy text).

Last but not the least, we need to close the file. Summing it up, your code will look like this:

Output

Let’s see the changes in the file abc.txt.

That’s how our own text editor works and looks like. I hope today you learned something new and understand how you can apply your programming skills to create some cool projects. If you have any questions, just leave a comment and I will be happy to help you.

Happy Coding!😎

Resources

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

Originally published at https://dev.to on December 22, 2020.

--

--

Shounak Das

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