Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 |
result = "" with open("./test.txt", "r+") as file: for line in file: if not line.isspace(): result += line file.seek(0) file.write(result) |
Code assumes that you have test.txt
in current directory with following content.
1 2 3 4 5 6 7 8 9 10 11 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. |
When you execute above program, you will get following result:
1 2 3 4 5 6 7 8 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. |
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.
1 2 3 4 5 6 7 8 |
result = "" with open("./test.txt", "r+") as file: result += "".join(line for line in file if not line.isspace()) file.seek(0) file.write(result) |
1 2 3 4 5 6 7 8 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. |
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.
1 2 3 4 5 6 7 8 9 10 |
result = "" with open("./test.txt", "r") as file: for line in file: if not line.isspace(): result += line with open("./new_test.txt", "w") as file: file.write(result) |
1 2 3 4 5 6 7 8 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
result = "" with open("./test.txt","r+") as file: for line in file.readlines(): if (len(line.strip()) == 0): continue if line: result += line file.seek(0) file.write(result) |
1 2 3 4 5 6 7 8 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. |
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.
1 2 3 |
with open("./test1.txt","r+") as file1, open("./test2.txt","r+") as file2 |
To maintain code readability, we can use the fileinput
module.
Further reading:
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 toTrue
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 beFalse
.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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import fileinput result="" with fileinput.FileInput( files=("./test1.txt", "./test2.txt"), inplace = False, backup ='.bak') as file: for line in file: if (len(line.strip()) == 0): continue if line: result += line with open("./two_files_data.txt", "w") as file: file.write(result) |
1 2 3 4 5 6 7 8 9 10 11 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six.It's line seven. It's line eight. It's line nine. It's line ten. |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import fileinput result="" with fileinput.FileInput( files=("./test1.txt", "./test2.txt"), inplace = False, backup ='.bak') as file: for line in file: if (len(line.strip()) == 0): continue if line: if (file.filename() == "./test2.txt" and file.filelineno() == 1): result += '\n' result += line with open("./two_files_data.txt", "w") as file: file.write(result) |
1 2 3 4 5 6 7 8 9 10 11 12 |
It's line one. It's line two. It's line three. It's line four. It's line five. It's line six. It's line seven. It's line eight. It's line nine. It's line ten. |
That’s all about how to remove empty lines from text file in Python.