简体   繁体   English

使用 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. I made a python program which gives me name of last modified file in a folder:-我制作了一个 python 程序,它为我提供了文件夹中最后修改文件的名称:-

  2. 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。

  3. 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.

相关问题 如何使用Python获取文件夹中上次修改文件的时间 - How to get the time of the last modified file in a folder using Python 使用Python提取最后修改日期,git存储库中文件的作者 - Extracting last modified date, author of files in git repository using python Python:如何打开最后修改的文件夹 - Python: how to open last modified folder Python列出最后10个修改的文件并读取所有10个文件的每一行 - Python listing last 10 modified files and reading each line of all 10 files 如何使用 Python 删除文件夹中除最后 5 个项目之外的所有文件 - How delete all files in a folder except last 5 items with Python 如何获取目录 Python 中最后 4 个修改的文件 - How to get the last 4 modified files in directory Python 使用 Python 下载 Sharepoint 文件夹中的所有文件? - Download all files in a Sharepoint folder using Python? 使用python删除临时文件夹中的所有文件 - Deleting all files in temp folder Using python 使用Python 2.7.5将文件夹中的所有压缩文件解压缩到同一文件夹 - Unzip all zipped files in a folder to that same folder using Python 2.7.5 如何使用 python 真正删除文件夹中的所有文件和 Windows 上的文件夹? - How to really delete all files in a folder and the folder on Windows using python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM