Remove Empty Lines from Text File in Python

Use isspace() Method

To remove empty lines from a Text File in Python:

  • Open a file in read and write mode(r+).
  • Use for loop for iterate over each line in the file.
  • Use isspace() method to check if line is empty. if it is not empty, add it to result.
  • Use seek(0) to move cursor to the start of the file.
  • Use write() method to write the result to the file.

Code assumes that you have test.txt in current directory with following content.

When you execute above program, you will get following result:

We created a string type variable result and initialize it with an empty string.

Next, the with statement is used to open the test.txt file in read and write mode (r+). The r+ mode is used to open a file to read and write and will produce an I/O error if the specified text file is not found.

Then, a for loop is used to iterate over each line of the test.txt file.

On each iteration, check if the current line is empty or not using the isspace() method.

The isspace() returns True if the current line has all whitespaces; otherwise False.

We used the seek() function to change the current file position to 0 because we wanted to overwrite the file with the data without empty lines.

Finally, the write() function is used to write into the file.

Another way of achieving the same results is to use join(), which joins all non-empty lines as follows.

If we don’t want to replace the data in the original file (test.txt) then we can create another file (new_test.txt) using another with statement.

Here, the w mode overwrites the existing file (if any) and creates a new file to write if it is not already there.

The above output will be saved in the new_test.txt file, the test.txt will remain unchanged. You can learn about different modes to read and write text files here.

Use strip() Method

The following code is similar to the above code examples except for one difference, here, we are using the strip() function which is used to remove trailing and leading whitespaces from the string.

To identify an empty line, we find the length of the line.strip() using the len() function and continue execution if it is 0; otherwise concatenate the line with result.

Finally, change the current file position to 0 to overwrite the file while the write() is used to write data to the file.

Till this point, we used the open() function to open a .txt file. What if we want to open multiple text files, remove empty lines and save the data without empty lines in a new text file?

For that, we will be using multiple open() functions as follows which will reduce the readability.

To maintain code readability, we can use the fileinput module.

Use the FileInput() Method

Here, we import the fileinput module to use its FileInput() method. We can use it as the context manager in the with statement. The FileInput() takes the following three parameters:

  • files – It denotes a tuple of multiple text files that we want to read.
  • inplace – It is set to True then the file content will be moved to the backup file. If we don’t want to lose data from the .txt files then, it must be False.
  • backup – This parameter is given to specify the extension of the backup file.

After that, we use the for loop to iterate over the data of both files, remove empty lines and concatenate non-empty lines with the result variable.

The important point is that we add a new line when the pointer starts reading the second file, otherwise, the last line of the test1.txt file and the first line of the test2.txt file would be on the same line. Don’t be confused, we will test both scenarios below using code examples.

See, we have It's line six from test1.txt and It's line seven from test2.txt on the same line. To get rid of this, we will use the if condition to add a new line character if we are on the first line of the test2.txt file.

That’s all about how to remove empty lines from text file in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *