How to remove trailing whitespace in code, using another script?
Something like:
import fileinput
for lines in fileinput.FileInput("test.txt", inplace=1):
lines = lines.strip()
if lines == '': continue
print lines
But nothing is being printed on stdout.
Assuming some string named foo
:
foo.lstrip() # to remove leading white space
foo.rstrip() # to remove trailing whitespace
foo.strip() # to remove both lead and trailing whitespace
Solution 1:
fileinput
seems to be for multiple input streams. This is what I would do:
with open("test.txt") as file:
for line in file:
line = line.rstrip()
if line:
print(line)
Solution 2:
You don't see any output from the print
statements because FileInput
redirects stdout
to the input file when the keyword argument inplace=1
is given. This causes the input file to effectively be rewritten and if you look at it afterwards the lines in it will indeed have no trailing or leading whitespace in them (except for the newline at the end of each which the print
statement adds back).
If you only want to remove trailing whitespace, you should use rstrip()
instead of strip()
. Also note that the if lines == '': continue
is causing blank lines to be completely removed (regardless of whether strip
or rstrip
gets used).
Unless your intent is to rewrite the input file, you should probably just use for line in open(filename):
. Otherwise you can see what's being written to the file by simultaneously echoing the output to sys.stderr
using something like the following (which will work in both Python 2 and 3):
from __future__ import print_function
import fileinput
import sys
for line in (line.rstrip() for line in
fileinput.FileInput("test.txt", inplace=1)):
if line:
print(line)
print(line, file=sys.stderr)
Solution 3:
If you're looking to tidy up for PEP8, this will trim trailing whitespace for your whole project:
import os
PATH = '/path/to/your/project'
for path, dirs, files in os.walk(PATH):
for f in files:
file_name, file_extension = os.path.splitext(f)
if file_extension == '.py':
path_name = os.path.join(path, f)
with open(path_name, 'r') as fh:
new = [line.rstrip() for line in fh]
with open(path_name, 'w') as fh:
[fh.write('%s\n' % line) for line in new]