[英]Archiving Files Using Python Apart from Latest File
I am trying to archive existing file apart from the latest modified file in Python or FME.除了 Python 或 FME 中的最新修改文件之外,我正在尝试存档现有文件。 I have managed to get it to point where I can get python pick up the latest modified file but any ideas on how I can archive all the files i have in my folder apart from the last modified file?我已经设法让它指出我可以让 python 获取最新修改的文件,但是关于如何归档我文件夹中的所有文件(除了最后修改的文件)的任何想法?
Thank You谢谢你
You can solve your problem using this snippet of code:您可以使用以下代码片段解决您的问题:
import glob
import os
import zipfile
files_dir = r'C:\Users\..\files' # here should be path to directory with your files
files = glob.glob(files_dir + '\*')
# find all files that located in specified directory
files_modify_dt = [os.path.getmtime(file) for file in files]
# take files except last modified file
files_to_zip = [file for _, file in sorted(zip(files_modify_dt, files))][:-1]
# zip of selected files
with zipfile.ZipFile(os.path.join(files_dir, 'archive.zip'), 'w', zipfile.ZIP_DEFLATED) as zip_obj:
for file in files_to_zip:
zip_obj.write(file, os.path.basename(file))
os.remove(file)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.