Replace text in file with Python

Posted by Aaron Hoffman on Stack Overflow See other posts from Stack Overflow or by Aaron Hoffman
Published on 2010-06-01T17:16:00Z Indexed on 2010/06/01 17:23 UTC
Read the original article Hit count: 301

Filed under:
|
|
|

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

© Stack Overflow or respective owner

Related posts about python

Related posts about file