Open In App

How to remove blank lines from a .txt file in Python

Last Updated : 30 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads