Find text then add line after in Python
Question: I need to read a plist file and search for a string, then add a new line of
text on the next line. I can't imagine it will take much to do this. However
the plist is in binary format so not exactly sure how to deal with that.

Thanks in advance,

Aaron

    
    
    #Convert plist to XML
    os.system('plutil -convert xml1 com.apple.iChat.Jabber.plist')
    
    AutoDiscovery = "<integer>0<integer>"
    
    import fileinput
    for line in fileinput.FileInput("com.apple.iChat.Jabber.plist",inplace=1):
       line = line.replace("<key>AutoDiscoverHostAndPort</key>",AutoDiscovery)
       print line,
    
    #Concert plist to binary file
    os.system('plutil -convert binary1 com.apple.iChat.Jabber.plist')
    


Answer: You want to convert it into xml format first:

    
    
    plutil -convert xml file.plist
    

Then the rest should be fairly easy.

EDIT:

    
    
    newFile = open('file.copy', 'w+')
    for line in open('file'):
        if (line.find('string_to_find') >= 0):
            # do something with "line"
        newFile.write(line)
    newFile.close()
    

EDIT2:

    
    
    # convert plist from binary to xml
    
    plist = plistlib.readPlist('your.plist')
    plist['key'] = 0
    plistlib.writePlist('your.plist')
    
    # convert plist from xml to binary
    

