views:

170

answers:

2

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

LINK to plist file: http://www.queencitytech.com/plist.zip

A: 

Your code worked for me fine. However, I would suggest a different approach: don't try overwriting the file directly. I never like changing the file directly because if you have a bug or something like that the file is lost. Generate a new file then copy it over manually (or within python, if you really want to).

PATH = 'testfile.txt'
FILE = open(PATH)
OUT_FILE = open('out_' + PATH, 'w')

for line in FILE.readlines():
   print >> OUT_FILE, line.replace('newhost', host),
orangeoctopus
My code worked for me as well but like I said it seemed to change the file somehow other than just the text. I was going to try your method and see if it responded differently. However, it is giving me a TypeError: cannot concatenate 'str' and 'file' objects. Any ideas whats wrong? Thanks, Aaron
Aaron
Oops -- fixed it.
orangeoctopus
That did fix the error. But still the output file doesn't want to open in the editor and it not recognized by the program. I added a link to the actual file which I am changing. I'm only replacing 'newhost'. One thing I did notice, if I manually edit the file via the terminal and replace 'newhost' and save the changes. This too does not let me open in the Editor or the Application (iChat).
Aaron
Can you open up your editor, make a trivial change, save, then undo that change and save, and see if you're able to open up the file still? (it should have been changed twice but otherwise identical to the original at this point).
Brenda Holloway
Yes, I tried doing this with the Text Wrangler editor I use and it worked fine. Through some more research, I learned that most plist files are in fact binary and therefore using the standard TextEdit will not work. I did find something called plistlib on the python website which looks like it can generate and parse Mac OS X .plist files. The only problem is I have no idea where to start. Literally, all I need to do is replace one thing in this plist file.
Aaron
Very interesting...I have something that can convert the plist binary file to xml and then back again. To convert a .plist file to XML format for editing:plutil -convert xml1 some_file.plistTo convert an XML .plist file to binary for use:plutil -convert binary1 some_other_file.plistHow would I go about using this in my python code? Do I convert it before I assign PATH? If so, will python just run the command and know what to do?
Aaron
A: 

Try using sys.stdout.write instead of print. readlines() retains the new line characters at the end of the read line. The print statement adds an additional new line character, so it's likely double spacing the file.

Andrew E. Falcon
The issue actually as to do with the fact the the .plist file is binary and it can't be edited the same way as a regular text document
Aaron
according to the fopen man page on my mac, the 'b' is ignored. UNIX systems don't make the distinction.
Andrew E. Falcon
looking at the file in a hex editor, the byte directly before the string "[email protected]" is the length of the string. Try modifying that to the length of the new string.
Andrew E. Falcon