2

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

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。

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

4

1 回答 1

3

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

for myPath in Path.cwd().iterdir():
    print(myPath.name)
于 2014-03-28T04:54:19.557 回答