Find&Replace using Python - Binary file

Posted by Aaron Hoffman on Stack Overflow See other posts from Stack Overflow or by Aaron Hoffman
Published on 2010-06-02T15:44:04Z Indexed on 2010/06/02 16:34 UTC
Read the original article Hit count: 262

Filed under:
|
|

Hello, I'm attempting to do a "find and replace" in a file on a Mac OS X computer. Although it appears to work correctly. It seems that the file is somehow altered. The text editor that I use (Text Wrangler) is unable to even open the file once this is completed.

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,

When I view the file from the terminal, it does say "testfile" may be a binary file. See it anyway? Is there a chance that this replace is corrupting the file? Do I have another option for this to work? I really appreciate the help.

Thank you, Aaron

UPDATE: the actual file is NOT a .txt file it is a .plist file which is preference file in Mac OS X if that makes any difference

© Stack Overflow or respective owner

Related posts about python

Related posts about text