Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

remove empty lines from file python

with open("new_file","r") as f:
 for i in f.readlines():
       if not i.strip():
           continue
       if i:
           print i,
Comment

python remove empty lines from file

import os


def remove_empty_lines(filename):
    if not os.path.isfile(filename):
        print("{} does not exist ".format(filename))
        return
    with open(filename) as filehandle:
        lines = filehandle.readlines()

    with open(filename, 'w') as filehandle:
        lines = filter(lambda x: x.strip(), lines)
        filehandle.writelines(lines)   
Comment

PREVIOUS NEXT
Code Example
Python :: python dict key delete 
Python :: python printing variables 
Python :: youtube-dl python get file name 
Python :: Get all the numerical column from the dataframe using python 
Python :: how to use csv in python 
Python :: webdriver firefox install 
Python :: plt.savefig specify dpi 
Python :: calculate the same value in list i python 
Python :: timedelta 
Python :: binary to decimal python 
Python :: cmd check if python is installed 
Python :: how to print a number at the end of a for loop in python 
Python :: shutdown flask server with request 
Python :: prime number python program 
Python :: import qq plot 
Python :: python hide details 
Python :: extract zip file in python zipfile 
Python :: library for converting text into image in python 
Python :: venv 
Python :: how to set background image in python tkinter 
Python :: pandas lamda column reference 
Python :: django orm sum 
Python :: pandas rename column values dictionary 
Python :: comment out a block in python 
Python :: colorbar font size python 
Python :: hex to rgb python 
Python :: python private 
Python :: apply lambda with if 
Python :: how to check the size of a file in python 
Python :: networkx draw graph with weight 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =