带有readPlist和attribute的弃用警告

2024-05-15 11:39:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到一种访问plist文件:/Library/Preferences的方法/com.apple.iPod.plist以访问其中的序列号。在

这是我当前的代码--

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'])

我不断地得到结果,但是一些快速的错误也出现了。我得到这个:

^{pr2}$

还有这个:

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

我猜使用load函数是相当简单的,尽管我花了很长时间才弄明白,使用我在网上找到的教程来修改它以满足我的需要。在

关于布尔属性错误,我不知道我做错了什么。在

谢谢!在


Tags: inimportrightcomappleforoslibrary
1条回答
网友
1楼 · 发布于 2024-05-15 11:39:00

要消除不推荐错误,请将包含readPlist的行替换为

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

您的第二个问题似乎是由于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.

我有一个类似的问题:AttributeError: 'dict' object has no attribute 'children'是通过用someObj['children']替换{}来解决的。我想您对right.values()的调用可能也有类似的情况,但是如果没有您期望的plist的实际示例,就很难判断。在

相关问题 更多 >