【问题标题】:DeprecationWarning with readPlist & AttributeError带有 readPlist 和 AttributeError 的 DeprecationWarning
【发布时间】:2018-09-05 03:20:22
【问题描述】:

我正在尝试找到一种方法来访问 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'])

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

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

还有这个:

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

我猜想使用 load 函数相当简单,虽然我很难使用网上找到的教程来修改它以满足我的需要。

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

谢谢!

【问题讨论】:

    标签: python boolean plist attributeerror


    【解决方案1】:

    要消除弃用错误,请将包含 readPlist 的行替换为

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

    您的第二个问题似乎是由于plistlib 的变化:

    3.7 版更改:结果中的字典值现在是普通字典。您不能再使用属性访问来访问这些字典的项目。

    我遇到了类似的问题:AttributeError: 'dict' object has no attribute 'children' 已通过将出现的 someObj.children[:] 替换为 someObj['children'] 来解决。我想您拨打right.values() 时可能会遇到类似的情况,但是如果没有您期望的 plist 的实际示例,就很难判断。

    【讨论】:

      【解决方案2】:

      我在转换为 python3 时遇到了同样的问题。我使用了 ne plistlb.load,但遇到下面的异常错误,我还没有找到解决方案。

      在 python2 中工作:

      #info = plistlib.readPlist(test_path)
      

      Python3:

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

      错误:

      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: 无效文件

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-06-23
        • 2021-02-02
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 1970-01-01
        • 2018-02-26
        • 2016-05-20
        相关资源
        最近更新 更多