【问题标题】:Why is path.name() in python 3.4 giving me "TypeError: 'str' object is not callable"?为什么 python 3.4 中的 path.name() 给我“TypeError:'str' object is not callable”?
【发布时间】:2016-01-18 23:57:04
【问题描述】:

以下代码只是试图打印目录中所有文件的名称:

from pathlib import Path

for myPath in Path.cwd().iterdir():
    print (myPath.name())

它给了我错误:

Traceback (most recent call last):
    File "NameTest.py", line 4, in <module>
        print (myPath.name())
TypeError: 'str' is not callable

如果我打印“myPath”对象类型,目录中的所有内容都会返回类 pathlib.windowspath。

我在最新版本的 parallels 中在 Windows 8 上使用 Python 3.4。

【问题讨论】:

    标签: python path typeerror python-3.4


    【解决方案1】:

    myPath.name 不可调用,因为它是str 类型的属性。 试试这个:

    for myPath in Path.cwd().iterdir():
        print(myPath.name)
    

    【讨论】:

    • 谢谢!我不知道为什么我没有看到。
    • Path(your_path).parent 有同样的东西 --> parentname 是 str 类型,不应该被称为 function()
    猜你喜欢
    • 2023-03-18
    • 2020-12-15
    • 1970-01-01
    • 2021-02-03
    • 1970-01-01
    • 2017-01-23
    • 2021-04-21
    • 2020-08-09
    • 2019-11-27
    相关资源
    最近更新 更多