Search code examples
pythonplistosx-yosemite

Plist file parsing error "'str' object has no attribute 'read'"


I'm trying to read a .plist file on Mac OSX with the plistlib. Sadly I always get an error when running the script

Traceback (most recent call last):
  File "/Users/johannes/pycharmprojects/adobe-cache-cleaner/test.py", line 6, in <module>
    pl = plistlib.load(fp2)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/plistlib.py", line 983, in load
    header = fp.read(32)

AttributeError: 'str' object has no attribute 'read'

that's my script:

import plistlib

fp2 = "/Users/Johannes/Pythonproject/test.plist"

pl = plistlib.load(fp2)
print pl

Solution

  • It looks like ptlistlib ist expecting a file not a string:

    import plistlib
    
    with open("/Users/Johannes/Pythonproject/test.plist", "rb") as file:
        pl = plistlib.load(file)
        print pl
    

    see https://docs.python.org/3/library/plistlib.html