Monday, 18 September 2017

How to remove empty rows from notepad using Python 2.7

Below script is used to remove empty rows from notepad.

#open the file in read mode which you need to modify, we  have used ip.txt file.
file= open("ip.txt","r")
# Read the file
lines = file.readlines()
# close the file
file.close()

# Read the file in write mode
file = open("ip.txt","w")
# Reading each line and writing text which is not a space back to the file.
for line in lines:
    if line!=""+"\n":
        file.write(line)
file.close()

Before the script execution, ip.txt contains below content. You can see there are various spaces in it.




















After script execution, All empty rows has been deleting from the file.











No comments:

Post a Comment