for line in (line.rstrip() for line in fileinput.FileInput('test.txt',
    inplace=1)):
    if line:
        print(line)
print(line, file=sys.stderr)
How to remove trailing whitespace in code, using another script?