删除超过7天的文件

我看到一些帖子删除特定文件夹中的所有文件(不是文件夹),但我根本不明白它们。

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

 Mypath = filesdataAPIArchiveFolder

有人有快速脚本,他们可以专门输入上面的路径来删除所有超过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))

另一个版本:

import os
import time
import sys

if len(sys.argv) != 2:
    print "usage", sys.argv[0], " <dir>"
    sys.exit(1)

workdir = sys.argv[1]

now = time.time()
old = now - 7 * 24 * 60 * 60

for f in os.listdir(workdir):
    path = os.path.join(workdir, f)
    if os.path.isfile(path):
        stat = os.stat(path)
        if stat.st_ctime < old:
            print "removing: ", path
            # os.remove(path) # uncomment when you will sure :)
链接地址: http://www.djcxy.com/p/42419.html

上一篇: Delete files that are older than 7 days

下一篇: How do I remove/delete a folder that is not empty with Python?