1
votes

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
What version of Python, and what version of iTunes? - abarnert
@davedwards Only in Python 2. Since the OP is using 3.x-style prints, and it's 2018, he's more likely to be using 3.x. - abarnert
@davedwards that is a previous version, it mentions it on a page that it is outdated, and while I run it. - Matthew Oujiri
Okay, good points, thanks. - chickity china chinese chicken
As a side note, historically, plistlib has added a lot of overhead over Expat, and also a lot of brittleness, so using it to parse iTunes library files is they're actually massive (as in hundreds of MB) was not really doable; you had to build something on top of, e.g, etree instead. I haven't really tried it with the new 3.4+ plistlib implementation, so maybe everything will work great… but keep your fingers crossed. - abarnert

2 Answers

1
votes

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

0
votes

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

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