0

示例代码

from pathlib import Path

for f in Path(<dir>).iterdir():
    print(f._str)

我正在使用它来传递给一个函数,但即使在正常运行或没有断点的情况下调试时它也不起作用。使用断点并单步执行它可以很好地打印所有内容(_str是总路径!

4

1 回答 1

1

我真的不确定您要做什么或打印什么,但是,是的,这会引发 AttributeError。可能是因为,嗯,._str不是Path类的属性。

from pathlib import Path

for f in Path('/tmp').iterdir():
    print(f._str)

AttributeError: _str

这确实打印了完整路径。

for f in Path('/tmp').iterdir():
    print(f)

/tmp/com.apple.launchd.0CERUFd5eE
/tmp/com.apple.launchd.JLaC2VPWPS
/tmp/com.apple.launchd.jyIh6h3f8I

如果您只想要文件和目录的名称,请执行print(f.name)

于 2016-01-27T20:51:48.440 回答