Clear File in Python

The quickest way to clear file contents in Python is by using the following code:

This code will open the file for writing and close it in the next line.

If there is no information entered, the file will erase contents and become empty, so there is no need for clearing the file before writing.

If there is no file, Python will create an empty text file.

This is the file we are going to use in the next examples:

Clear file if exists

If you don’t want Python to create a file if it doesn’t exist, you have to check whether the file is in the specified location before writing to a file.

First, Python tries to open the file for reading. If it fails, it automatically returns an exception under except. If there is a file, it will be opened for reading and after that for writing.

The file is closed before anything is written to it, so all data inside the file is erased.

Clear the file after reading

Now, let’s try to read the file and clear it after that.

Each line inside the file is saved as a list element before clearing file contents.

The printed result is:

['First line', 'Second line', 'Third line']

Clear file line by line

You can also clear a file line by line:

First, this code reads lines from the file to a list. Next, for each of these lines, it writes an empty string, therefore clearing these lines.

Erase the last line

To remove the last line from the file, first, we have to count the number of lines in a file. Then we have to write an empty string to the last one.

Here’s the code:

The following line returns the number of text lines in a file:

We have to subtract one line because the indexing in the loop starts from 0 and not from 1.

The enumerate function returns the index of the current loop and a value. If the index is the same as the last_line – 1 then it writes “” instead of the current line.

Remove selected line

In a similar way, you can give a number of the line you want to remove. Let’s say you want to remove the second line from the file.

Remove selected lines

If you want to delete more than a single line, you can use a list of lines to remove. Let’s modify our file a bit by adding additional lines:

Run the code to remove multiple lines. In our case, they will be 2, 5, and 7.

First, there are three numbers added to a list. These are the numbers of lines we want to remove.

Later in the code, we check if the current line is inside the list. If the condition is met, the line is removed from the file.

If you open the file, you will notice that the selected lines are removed.

Clear lines with the provided string

You can remove lines that are the same as provided text.

The file content is saved as list elements.

In the next part, Python checks for each line whether the string equals the “Send line”. If it’s not then it writes it to a file, otherwise, it drops it.

The newline characters are stripped from the file for each line.

This is what our new file looks like:

You can also use substrings, instead of whole strings.

Just replace this line:

With this one:

This code will remove all lines that contain the word “Second”. It’s case-sensitive.

The result is the same as in the previous example.