문제

The following code is just trying to print the name of all files in a directory:

from pathlib import Path

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

It gives me the error:

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

If I print the "myPath" object type, everything in the directory returns class pathlib.windowspath.

I'm using Python 3.4 on Windows 8 in the latest version of parallels.

도움이 되었습니까?

해결책

myPath.name isn't callable, because it's an attribute of str type. Try this instead:

for myPath in Path.cwd().iterdir():
    print(myPath.name)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top