python脚本 – 删除指定天数前的文件

python

python;gutter:true;"># -*- coding: utf-8 -*-

# Author: Jack

# Date: 2018/02/09 15:33

import os

import sys

import time

# 删除某个时间点之前的文件

def del_files(before_times, path=”.”):

for root, dirs, files in os.walk(path):

for f in files:

full__path_file = os.path.join(root, f)

if os.path.getmtime(full__path_file) < before_times:

try:

os.remove(full__path_file)

print(“[Succeed]: %s delete success.” % full__path_file)

except Exception as error:

print(“[Failed]: %s delete failed,%s” % (full__path_file, error))

if __name__ == “__main__”:

try:

del_dir = “D:\\test3” # 要操作删除的文件夹

before_days = int(1) # 设定删除几天前的文件

before_time = time.time() – 3600 * 24 * before_days

del_files(before_time, del_dir)

except Exception as e:

print(“py script error:%s” % e)

sys.exit(-1)

以上是 python脚本 – 删除指定天数前的文件 的全部内容, 来源链接: utcz.com/z/386795.html

回到顶部