使用 python 检查文件夹中所有最后修改的文件的程序?

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)
  1. 我制作了一个 python 程序,它给出了文件夹中最后修改的文件的名称:-

  2. 这个程序 运行 很好,但问题是如果我放置新文件或者如果我正在编辑文件夹中的任何文件而不是给我正确的输出但是如果我是将任何文件复制到文件夹中,但我没有得到任何输出。

  3. 以及如何使用此程序显示文件夹中所有最后修改的文件。

5.So 如果我将任何文件复制到文件夹,而不是我没有获取文件名,并且我无法显示文件夹中所有最后修改的文件,那么这个程序基本上有两个问题。

在 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)

key函数就是抓取修改时间和创建时间中最大的那一个,然后把那个最大的时间作为key。