DeprecationWarning with readPlist & AttributeError
Asked Answered
B

2

6

I am trying to find a way to access the plist file: /Library/Preferences/com.apple.iPod.plist to access the serial numbers in it.

Here is my current code--

import os
import plistlib

fileName=os.path.expanduser('/Users/Ryan/Library/Preferences/com.apple.iPod.plist')

pl=plistlib.readPlist(fileName)

for left, right in pl.items(): 
   for values in right.values():
         print(values['Serial Number'])

I keep getting the results but some quick errors pop up as well. I get this one:

plist.py:8: DeprecationWarning: The readPlist function is deprecated, use load() instead pl=plistlib.readPlist(fileName)

and also this one:

  File "plist.py", line 16, in <module>
    for values in right.values():
   AttributeError: 'bool' object has no attribute 'values'

I am guessing the using the load function is fairly simple, although I have had a hard time figuring it out using the tutorials I have found online to modify it for my needs.

Regarding the boolean AttributeError, I have no idea what I am doing wrong.

Thanks!

Brita answered 5/9, 2018 at 3:20 Comment(0)
D
8

To get rid of the deprecation error, replace the line containing readPlist with

with open(fileName, 'rb') as f:
    pl = plistlib.load(f)

Your second problem seems to be due to a change in plistlib:

Changed in version 3.7: Dict values in the result are now normal dicts. You no longer can use attribute access to access items of these dictionaries.

I had a similar problem: AttributeError: 'dict' object has no attribute 'children' was solved by replacing occurences of someObj.children[:] with someObj['children']. I suppose something similar might be the case with your call to right.values(), but it is hard to tell without an actual example of the plist you expect.

Dual answered 29/10, 2018 at 9:53 Comment(0)
B
-1

I got the same problem when converting to python3. I uses the ne plistlb.load, but encounter exception error below that I haven't able to find a solution yet.

Work in python2:

#info = plistlib.readPlist(test_path)

Python3:

with open(test_path, 'rb') as fp:
    pl = plistlib.load(fp)
print(test_path)

Error:

Traceback (most recent call last):
  File "/path_to_python.py", line 70, in <module>
    pl = plistlib.load(fp)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/plistlib.py", line 869, in load
    raise InvalidFileException()

plistlib.InvalidFileException: Invalid file

Bettyannbettye answered 21/1, 2022 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.