I'm trying to replace some text in a file with a value. Everything works fine but when I look at the file after its completed there is a new (blank) line after each line in the file. Is there something I can do to prevent this from happening.
Here is the code as I have it:
  import fileinput
    for line in fileinput.FileInput("testfile.txt",inplace=1):
       line = line.replace("newhost",host)
       print line
Thank you, Aaron
Each line is read from the file with its ending newline, and the print adds one of its own.
You can:
print line,
Which won't add a newline after the line.
The print line automatically adds a newline. You'd best do a sys.stdout.write(line) instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With