How to parse plists in Python?

1.2k views Asked by At

I'm trying to parse through a massive file for an iTunes library and I'm attempting to use plistlib library.

My code looks something like this:

import plistlib

with open('iTunes Music Library.xml') as fp:
    pl = plistlib.load(fp)

print(pl["aKey"])

As I do so, I get the error:

TypeError: startswith first arg must be str or a tuple of str, not bytes

I'm not sure what this is, any explanations?

2

There are 2 answers

1
isopach On

You need to open the file as a binary, i.e.

with open('iTunes Music Library.plist', 'rb') as fp:
        pl = plistlib.load(fp)
2
user2357112 On

As stated in the docs, plistlib.load takes a binary file object. You've given it a file opened in text mode.