虽然我在目录中有文件,但是代码没有返回任何东西,有人能帮我吗?
from pathlib import Path
date_creation = lambda f: f.stat().st_ctime
directory = Path('directory')
files = directory.glob('*.py')
sorted_files = sorted(files, key = date_creation, reverse = True)
for f in sorted_files:
print(f)
发布于 2019-02-12 14:03:47
请注意,传递给Path()
的参数被解释为相对路径,而不是绝对路径。
这意味着,在运行此代码时,您将在当前目录中寻找一个名为“目录”的子目录。
基于这一理解,请将正确的参数传递给Path()
。这会给你带来结果的。
例如,在我的机器上,以下使用绝对路径的代码运行良好:
from pathlib import Path
date_creation = lambda f: f.stat().st_ctime
directory = Path('F:/MyParentFolder/MySubFolder')
files = directory.glob('*.py')
sorted_files = sorted(files, key = date_creation, reverse = True)
for f in sorted_files:
print(f)
https://stackoverflow.com/questions/54659037
复制相似问题