gpt4 book ai didi

python - 删除超过 7 天的文件

转载 作者:太空狗 更新时间:2023-10-30 01:41:27 25 4
gpt4 key购买 nike

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

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

 Mypath = \\files\data\APIArchiveFolder\

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

最佳答案

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

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

关于python - 删除超过 7 天的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com