如何在python中获得存档列表的最后更改时间并比较它们?



我试图获得文件夹中一组文件的更改时间。每次任何归档文件更改时,我都需要自动创建上次修改的归档文件的备份副本。

我的测试:

我已经尝试了以下代码来获取更改的日期/时间,我需要列出它来比较。

import os
import shutil
from pathlib import Path
import os.path, time

class edit:
def __init__(last_edit, chage, creation):
last_edit.change = list
last_edit.creation = list

file_soure = 'C:/Users/Automation/Desktop/NEW2/'
#file_destination = 'C:/Users/Automation/Desktop/NEW/'
get_files = os.listdir(file_source)
for x in range(2,len(get_files)):
last_edit.change = time.ctime(os.path.getmtime(x + get_files))

print(last_edit.change)
for g in get_files:
shutil.copy(file_source + g, file_destination)

结果:

['Teste.txt', 'Teste2.txt']
Traceback (most recent call last):
File "c:UsersAutomaçãoDesktopScriptsPythonTeste.py", line 29, in <module>
print(last_edit.change)
NameError: name 'last_edit' is not defined
PS C:UsersAutomaçãoDesktopScriptsPython> 

对象没有声明?

我刚刚找到了我需要做的事情。

import os
import shutil
import filecmp
from datetime import datetime
try:    
file_source = 'D:/00-Backup/99. Temp/'
file_comparator = 'D:/00-Backup/01. PLC/'
get_files = os.listdir(file_source)
cmp_files = os.listdir(file_comparator)
os_date = datetime.now().strftime('%Y-%m-%d')
plc_name = "PLC1"
common = get_files

match,missmatch,errors = filecmp.cmpfiles(file_source, file_comparator,common, shallow=True)
print(match)
print(missmatch)
print(errors)
if errors[0] in get_files:
os.makedirs('D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date)
file_destination = 'D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date
for g in get_files:
shutil.copy(file_source + g, file_destination)
elif missmatch in get_files:
os.makedirs('D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date)
file_destination = 'D:/00-Backup/01. PLC/'+ plc_name + "-" + "%s" % os_date
for g in get_files:
shutil.copy(file_source + g, file_destination)
except IndexError:
pass