删除超过 7 天的文件 [英] Delete files that are older than 7 days

查看:75
本文介绍了删除超过 7 天的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过一些帖子,删除特定文件夹中的所有文件(不是文件夹),但我就是不明白.

I have seen some posts to delete all the files (not folders) in a specific folder, but I simply don't understand them.

我需要使用 UNC 路径并删除所有超过 7 天的文件.

I need to use a UNC path and delete all the files that are older than 7 days.

 Mypath = \\files\data\APIArchiveFolder\

是否有人有快速脚本,他们可以专门输入上面的路径来删除所有超过 7 天的文件?

Does someone have quick script that they can specifically input the path above into that would delete all files older than 7 days?

推荐答案

此代码删除当前工作目录中 >= 7 天前创建的文件.风险自负.

This code removes files in the current working directory that were created >= 7 days ago. Run at your own risk.

import os
import time

current_time = time.time()

for f in os.listdir():
    creation_time = os.path.getctime(f)
    if (current_time - creation_time) // (24 * 3600) >= 7:
        os.unlink(f)
        print('{} removed'.format(f))

这篇关于删除超过 7 天的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆