32

For example, we have some file like that:

first line
second line

third line

And in result we have to get:

first line
second line
third line

Use ONLY python

wjandrea
  • 28,235
  • 9
  • 60
  • 81
user285070
  • 761
  • 2
  • 12
  • 21

10 Answers10

49

The with statement is excellent for automatically opening and closing files.

with open('myfile','r+') as file:
    for line in file:
        if not line.isspace():
            file.write(line)
Barranka
  • 20,547
  • 13
  • 65
  • 83
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114
  • 7
    +1 for use of "with" and good, pythonic iteration through lines, in addition to not mutating the good output lines. – Michael Aaron Safyan Mar 03 '10 at 21:54
  • 12
    It seems that you have to open myfile with `r+` flag instead of `rw` according to https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files – furins Jan 22 '15 at 09:40
  • 4
    This solution seems like it would have buffering/overwrite problems for any file larger than the input buffer size. And if it doesn't, can somebody explain why? – David K. Hess Sep 08 '16 at 20:13
  • This works fine for everything except removing blank lines already existing between the data in text file. – Shri Nov 12 '21 at 08:46
  • 7
    Python 3: ValueError: must have exactly one of create/read/write/append mode. Does this solution really work? – Raymond Dec 28 '21 at 19:12
37
import fileinput
for line in fileinput.FileInput("file",inplace=1):
    if line.rstrip():
        print line
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
  • 2
    +1 for also catching lines that contain whitespace and nothing else. – Tim Pietzcker Mar 03 '10 at 07:42
  • 2
    This will change for formatting of whitespace even in the good lines – Thomas Ahle Mar 03 '10 at 07:53
  • 3
    Markdown formatting utilises trailing spaces. Remove simple change to this answer would strip lines with just whitespace and preserve trailing spaces: `if line.rstrip(): print line` – MattH Mar 03 '10 at 09:15
  • @Thomas, and why would a field have an ending "\n" in a file? If a file has "\n", then i would bet its literal. If its really a "\n", then the next field will be on the next line. isn't that so? or am i still missing what you are saying? If its necessarily pls provide your explanation as an answer as the putting in comment is hard to read. – ghostdog74 Mar 03 '10 at 10:51
  • @ghostdog74 It's not about the line breaks, but about the tabs. If you cut the tabs from the end of each line, then each row in the table will not have the same number of columns. – Thomas Ahle Mar 03 '10 at 22:55
  • This doesn't work correctly for me. If I have N consecutive blank lines, it removes N-1. If there's only 1 blank line, it is not removed. The example in the question therefore would not be handled. I'm using python on Windows, if that matters. – gigaplex Oct 08 '15 at 00:45
  • This script removes every after the 3rd line for my file. – Zingg Nov 20 '21 at 05:38
13
import sys
with open("file.txt") as f:
    for line in f:
        if not line.isspace():
            sys.stdout.write(line)

Another way is

with open("file.txt") as f:
    print "".join(line for line in f if not line.isspace())
John La Rooy
  • 295,403
  • 53
  • 369
  • 502
5
with open(fname, 'r+') as fd:
    lines = fd.readlines()
    fd.seek(0)
    fd.writelines(line for line in lines if line.strip())
    fd.truncate()
patke pravin
  • 69
  • 1
  • 2
4

I know you asked about Python, but your comment about Win and Linux indicates that you're after cross-platform-ness, and Perl is at least as cross-platform as Python. You can do this easily with one line of Perl on the command line, no scripts necessary: perl -ne 'print if /\S/' foo.txt

(I love Python and prefer it to Perl 99% of the time, but sometimes I really wish I could do command-line scripts with it as you can with the -e switch to Perl!)

That said, the following Python script should work. If you expect to do this often or for big files, it should be optimized with compiling the regular expressions too.

#!/usr/bin/python
import re
file = open('foo.txt', 'r')
for line in file.readlines():
    if re.search('\S', line): print line,
file.close()

There are lots of ways to do this, that's just one :)

Chirael
  • 3,025
  • 4
  • 28
  • 28
  • 2
    You can do commandline scripts with python using the `-c` flag. Unfortunately you would have to use multiple lines (or seperation with ;) in order to read from standard input. – Thomas Ahle Mar 03 '10 at 07:58
1
>>> s = """first line
... second line
... 
... third line
... """
>>> print '\n'.join([i for i in s.split('\n') if len(i) > 0])
first line
second line
third line
>>> 
Djangonaut
  • 5,511
  • 7
  • 40
  • 53
  • It depends on what "blank" means - this only works if blank means "nothing at all". If there are spaces between second line and third line, this will fail. Plus it needs to work on files :) But I like that you didn't have to import regexps :) – Chirael Mar 03 '10 at 07:42
  • @Chirael - for that case you may add just len(i.strip()) > 0 – Djangonaut Mar 03 '10 at 07:44
1

You can use below way to delete all blank lines:

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

We can also write the output to file using below way:

with open("new_file","r") as f, open("outfile.txt","w") as outfile:
 for i in f.readlines():
       if not i.strip():
           continue
       if i:
           outfile.write(i)            
Aashutosh jha
  • 552
  • 6
  • 8
0

Have you tried something like the program below?

for line in open(filename):
    if len(line) > 1 or line != '\n':
        print(line, end='')
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
0

Explanation: On Linux/Windows based platforms where we have shell installed below solution may work as "os" module will be available and trying with Regex

Solution:

import os
os.system("sed -i \'/^$/d\' file.txt")
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
patke pravin
  • 69
  • 1
  • 2
  • 2
    1. This ignores a constraint of the question (i.e. use ONLY python) and 2. What if it isn't a POSIX system? – Jared Smith Apr 22 '19 at 18:17
  • @Jared Smith The Answer which i have posted is for LSB based systems which is superset of POSIX. – patke pravin Apr 22 '19 at 18:28
  • It'd be great if you could add more details/explanations to your answer. – ahajib Apr 22 '19 at 19:09
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-‌​code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. – nircraft Apr 22 '19 at 20:35
  • you're basically not using python :) – Lethargos Oct 15 '21 at 15:09
  • Doesn't work. sed: 1: "file.txt": command a expects \ followed by text – Raymond Dec 28 '21 at 19:18
0

Using List Comprehension:

with open('your_file.txt', 'r') as f:
    result = [line.strip() for line in f.readlines()]
    print(result)

Using Normal loop :

with open('your_file.txt', 'r') as file:
    for line in f.readlines():
        print(line.strip())