使用pathlib检索最新文件
我主要使用 pathlib 而不是 os 作为路径,但是有一件事我未能在 pathlib 上工作。 例如,如果我需要目录中最新创建的 .csv,我将使用 glob & os
import glob
import os
target_dir = glob.glob("/home/foo/bar/baz/*.csv")
latest_csv = max(target_dir, key=os.path.getctime)
有没有仅使用 pathlib 的替代方案? (我知道您可以使用 Path.glob(*.csv)
生成匹配文件)
I primarily use pathlib over os for paths, however there is one thing I have failed to get working on pathlib.
If for example I require the latest created .csv within a directory I would use glob & os
import glob
import os
target_dir = glob.glob("/home/foo/bar/baz/*.csv")
latest_csv = max(target_dir, key=os.path.getctime)
Is there an alternative using pathlib only?
(I am aware you can yield matching files with Path.glob(*.csv)
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以通过执行以下操作在 pathlib 中实现它:
Path 对象有一个名为 stat() 的方法,您可以在其中检索创建和修改日期等内容。
You can achieve it in just pathlib by doing the following:
A Path object has a method called stat() where you can retrieve things like creation and modify date.