How to remove blank lines from a .txt file in Python
Last Updated :
28 Apr, 2025
Many times we face the problem where we need to remove blank lines or empty spaces in lines between our text to make it look more structured, concise, and organized. This article is going to cover two different methods to remove those blank lines from a .txt file using Python code.
This is going to be achieved using the following main methods:
Creating a Test .txt File
First, we will create a file in .txt format where we will leave some missing or blank lines that can be removed later. The following code is used to create and save a file using Python only.
Python3
# message to be written to the .txt file
msg = '''Hello Programmers \
\n\n\n\n\n GFG is the best \
platform \n\n\n\n\n This \
file is a sample for code \
in Python\n\n\n Follow GFG \
website for more updates'''
# Creating a .txt file in Python
with open('gfg.txt', 'w') as f:
f.write(msg)
You would find the text file saved in your system with the same name. The following are the contents of the .txt file:
Hello Programmers
GFG is the best platform
This file is a sample for code in Python
Follow GFG website for more updates
How to remove blank lines from a .txt file in Python using the str.strip() Method
The str.strip() method in python removes all the spaces around the input string and returns the resulting string.
We are going to use the following approach to carry out our desired task:
- Open the input and output .txt files in read and write mode(r,w).
- Using for loop to perform iteration.
- Using str.strip() method to check if the line is empty after removing all the spaces from it.
- The lines containing only spaces and empty string are filtered out.
- Use the write() method to write the result of the file.
The following is an implementation of the above approach:
Python3
# opening and creating new .txt file
with open(
"gfg.txt", 'r') as r, open(
'output.txt', 'w') as o:
for line in r:
#strip() function
if line.strip():
o.write(line)
f = open("output.txt", "r")
print("New text file:\n",f.read())
Output:
Hello Programmers
GFG is the best platform
This file is a sample for code in Python
Follow GFG website for more updates
In general, strip ( ) removes characters from both left and right based on the argument of the string (specifying the set of characters to be removed) but as we can see that no such argument has been provided thus strip ( ) will remove all the whitespaces in the text. That's why all the blank lines have been removed.
How to remove blank lines from a .txt file in Python using the str.isspace() Method
The str.isspace() method checks whether the specified character is whitespace or not and returns a boolean.
We are going to use the following approach to carry out our desired task:
- Open the input and output .txt files in read and write mode(r,w).
- Using for loop to perform iteration.
- Using isspace() method to check if the line is empty. Non empty lines are added  to result.
- Use the write() method to write the result of the file.
The following is an implementation of the above approach:
Python3
# opening and creating new .txt file
with open(
"gfg.txt", 'r') as r, open(
'output.txt', 'w') as o:
for line in r:
#isspace() function
if not line.isspace():
o.write(line)
f = open("output.txt", "r")
print("New text file:\n",f.read())
Output:
Hello Programmers
GFG is the best platform
This file is a sample for code in Python
Follow GFG website for more updates
Similar Reads
How To Remove Blank Lines From a .txt File In Node.js? Removing blank lines from a text file is a common task when working with data in text format. Blank lines can occur due to formatting errors, data entry mistakes, or during file generation. Node.js, with its non-blocking I/O and file system capabilities, makes it easy to automate this process.In thi
3 min read
How to remove brackets from text file in Python ? Sometimes it becomes tough to remove brackets from the text file which is unnecessary to us. Hence, python can do this for us. In python, we can remove brackets with the help of regular expressions. Syntax: # import re module for using regular expression import re patn =  re.sub(pattern, repl, sent
3 min read
How to read specific lines from a File in Python? Text files are composed of plain text content. Text files are also known as flat files or plain files. Python provides easy support to read and access the content within the file. Text files are first opened and then the content is accessed from it in the order of lines. By default, the line numbers
3 min read
How to Read from a File in Python Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently.Example File: geeks.txtHello World Hello GeeksforGe
5 min read
Remove last character from the file in Python Given a text file our task is to delete the last character from the file using Python Example: Input: File before removing the last character GeeksforGeeks Output: File after removing the last character GeeksforGeekExample 1: Using the seek and truncate functionPython3 with open('geeks.txt','w') as
2 min read