【发布时间】:2019-05-02 15:37:27
【问题描述】:
我正在尝试监视日志文件夹。如果创建了任何新文件,则必须返回文件路径。为此,我使用了以下代码:
import glob
list_of_files_in_real_predicted = glob.iglob(r'logging\real_predicted\*')
latest_file_in_real_predicted = max(list_of_files_in_real_predicted, key=os.path.getctime)
print(latest_file_in_real_predicted)
返回的输出是:logging\real_predicted\log935.csv
而不是:logging\real_predicted\log0.csv
请告诉我如何获取最新创建的文件。
【问题讨论】:
-
截图中显示的是
Date modified,所以你必须使用os.path.getmtime来获取这个值。 -
我建议添加
os.stat('log0.csv')和os.stat('log935.csv)的输出,因为os.path.getctime在下面使用os.stat。
标签: python python-3.x file