python remove empty lines from file

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

3.78
9
Awgiedawgie 440220 points

                                    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)   

3.78 (9 Votes)
0
3.57
7
Awgiedawgie 440220 points

                                    with open(fname, 'r+') as fd:
    lines = fd.readlines()
    fd.seek(0)
    fd.writelines(line for line in lines if line.strip())
    fd.truncate()

3.57 (7 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
remove blank lines from text data python Detect and remove emptylines at the top or bottom of the file. in python how to remove empty lines from readlines python python remove empty lines from a file python remove blank lines in text file python remove empty new lines from text python read file and remove blank line identify blank line in file and remove in pythn how to remove empty lines in a file python delete empty lines in text file python remove blank lines from text file python remove empty lines from text python python remove empty lines from text get rid of empty lines python python remove all empty lines from file remove empty lines with python python remove empy lines from file Remove empty line in file python python remove blank lines from a file python clean lines in file how to remove blank lines from a file in python python 3 remove empty newline from file python 3 remove blank lines from file python open file remove empty lines in loop python open file remove empty lines remove blank lines in file in python remove blank line from txt file python delete final blank lines file python python open file file remove empty lines python remove empty lines at end of file python remove a blank line in text file python file delete empty lines python remove blank lines from file python remove empty lines python delete empty lines delete blank line in a file python remove empty lines in a file python python remove empty line in txt python remove empty line txt how to remove empty lines python removing empty line in text file python delete empty lines python how to delete all the empty lines from a line inpythin$ python remove empty lines from file only while reading how to remove empty lines in text file python get rid of empty lines in text file python python remove empty lines in text file python3 remove empty lines windows remove blacnk line in file python remove empty lines from file python stream file remove blank line python python remove extra lines line.strip without blank space python strip empty lines remove mepty lines from string remove empty lines from string python delete empty line in python file remove empty lines from text file python python remove empty line from file python remove empty lines from file remove blank lines python Python remove blank lines from text file how to remove empty lines from a file in python remove empty line python remove blank lines from text python remove all empty lines python how to remove blank lines in python how to delete empty lines in python delete empty lines python file python remove empty lines from text file HOW TO DELETE BLANK ROWS FROM TEXT FILE IN SPYDER how to remove empty lines from file in python how to remove last empty lines in text file python remove last empty line from file python remove empty line in last line from file python remove empty line from file python Python get rid of extra lines in text file python text file remove empty lines open the text file and remove the last empty line in python how to remove empty line at end of file in file python how to remove empty line in file python python write to file remove empty line remove empty lines python remove all empty lines from file python python delete empty lines from file how to remove empty lines from text file in python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source