[英]Program to check all last modified files in a folder using python?
import glob
import os
import time
Path = 'Aabmatica/*'#Folder path
list_of_files = glob.glob(Path) # * Name of the folder in which all files exist
latest_file = max(list_of_files, key=os.path.getmtime)
print()
print("last modified/added file is:",end=" ")
print(latest_file)
print()
modification_time = os.path.getmtime(latest_file)
local_time = time.ctime(modification_time)
print("modified time: ",local_time)
我制作了一个 python 程序,它为我提供了文件夹中最后修改文件的名称:-
该程序运行良好,但问题是,如果我放置新文件或者我正在编辑文件夹中的任何文件,那么它会给我正确的 output 但如果我将任何文件复制到文件夹中,我不会得到任何 output。
以及如何使用该程序显示文件夹中所有最后修改的文件。
5.因此,如果我将任何文件复制到文件夹中,则该程序基本上存在两个问题,而不是我没有得到文件名,并且我无法显示文件夹中所有最后修改的文件。
在 Windows 中,文件的副本可能具有新的创建时间。 您可以查看os.path.getctime()
来检查文件副本的创建时间。
如果按预期工作,那么您可以将os.path.getctime()
作为附加检查包含在max()
的键中。
def latest_change(filename):
return max(os.path.getmtime(filename), os.path.getctime(filename))
latest_file = max(list_of_files, key=latest_change)
密钥 function 只是抓取修改或创建时间最长的那个,然后使用那个最长的时间作为密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.