【问题标题】:Attribute error when I use the readable function in Python在 Python 中使用可读函数时出现属性错误
【发布时间】:2022-10-20 22:02:56
【问题描述】:

这是代码:


K = open("text.txt", "r")
print(K.readable())

文本文件的名称是 text.txt,python 脚本的名称是 ex.py。 python 脚本和文本文件在同一目录中。现在,当我去 powershell 运行 python 脚本时,我得到以下代码:


 File "ex.py", line 2, in <module>
    print(K.readable())
AttributeError: 'file' object has no attribute 'readable' 

错误的原因是什么?

我仍然是初学者,我希望我的问题的答案尽可能简单。

【问题讨论】:

    标签: python-2.7 attributeerror


    【解决方案1】:

    我想我偶然发现了我的问题的答案。我收到错误的原因是因为 python 2.7 不支持函数“可读()”。我知道是这种情况,因为当我尝试在使用 python 3 的环境中运行代码时,错误消失了。

    Powershell 使用 python 2.7,当我尝试运行代码时它总是打印错误。但是当我在 Visual Studio 上运行代码时,我没有发现任何错误,原因是 Visual Studio 使用的是 python 3。

    简而言之,python 2 不支持“readable()”函数,而 python 3 支持它。

    【讨论】:

      【解决方案2】:

      尝试

      python3 ex.py
      

      代替

      python ex.py
      

      【讨论】:

        猜你喜欢
        • 2021-07-18
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 2021-05-30
        • 1970-01-01
        • 2020-11-30
        • 2021-07-01
        • 2021-08-07
        相关资源
        最近更新 更多