[英]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)
I made a python program which gives me name of last modified file in a folder:-我制作了一个 python 程序,它为我提供了文件夹中最后修改文件的名称:-
This program is running well but the problem is that if I place new file or if I am editing any file in a folder than it is giving me correct output but if I am copying any file into the folder than I am not getting any output.该程序运行良好,但问题是,如果我放置新文件或者我正在编辑文件夹中的任何文件,那么它会给我正确的 output 但如果我将任何文件复制到文件夹中,我不会得到任何 output。
And how can I show all the last modified files from folder using this program.以及如何使用该程序显示文件夹中所有最后修改的文件。
5.So there is basically two problem with this program if I am copying any file into folder than I am not getting the file name and I am unable to show all the last modified files from folder. 5.因此,如果我将任何文件复制到文件夹中,则该程序基本上存在两个问题,而不是我没有得到文件名,并且我无法显示文件夹中所有最后修改的文件。
In Windows a copy of a file probably has a new creation time.在 Windows 中,文件的副本可能具有新的创建时间。 You can look at os.path.getctime()
to check the creation time for the copy of the file.您可以查看os.path.getctime()
来检查文件副本的创建时间。
If that works as expected then you could include os.path.getctime()
as an additional check in the key to max()
.如果按预期工作,那么您可以将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)
The key function just grabs whichever of the modification or creation time is greatest, and then uses that greatest time as the key.密钥 function 只是抓取修改或创建时间最长的那个,然后使用那个最长的时间作为密钥。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.