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 :: replace number with string python 
Python :: python set recursion limit 
Python :: python day of the week 
Python :: python requests port 
Python :: python csv dict reader 
Python :: python get number of days 
Python :: python os remove extension 
Python :: python more order of columns 
Python :: how to smooth a function in python 
Python :: how to check if file exists pyuthon 
Python :: scapy python import 
Python :: string startswith python 
Python :: convert decimal to binary in python 
Python :: make dataframe index a column 
Python :: split list in half python 
Python :: pandas check if value in column is in a list 
Python :: kfold cross validation sklearn 
Python :: check if string has digits python 
Python :: how to underline text in tkinter 
Python :: how to print x in python 
Python :: pandas dataframe column names 
Python :: catch error python 
Python :: Row wise mean pandas 
Python :: python get element from list 
Python :: How to get the value of an Entry widget in Tkinter? 
Python :: add custom field to serializer 
Python :: tkinter frame example 
Python :: how to use dictionary comprehension to make a dictionary for some names of a list in python 
Python :: stock market api python 
Python :: python dictonary of dictonary 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =