windows server清理文件

# -*- coding: utf-8 -*-

"""
@Time :2020/06/24
@Author :Mr.Yang
@File :windows_del.py
@Software :PyCharm
@Description :对指定目录下的文件进行清理
"""

import os
import time

"""设置旧文件X天删除"""
Days = 180

"""设置需要清理目录"""
Path = 'D:\shell'

def delfile(Path):
for file in os.listdir(Path):
"""获取指定目录下的目录及文件"""
filename = os.path.join(Path,file)
if os.path.isfile(filename):
"""获取当前文件时间"""
alter_time = os.stat(filename).st_mtime
"""计算X天前的时间"""
last_time = time.time() - 3600 * 24 * Days
"""判断X天前时间是否大于文件时间"""
if last_time > alter_time:
os.remove(filename)
print "del %s success" % filename

if __name__ == '__main__':
delfile(Path)
posted @ 2020-06-24 14:28  蜡笔没小新  阅读(263)  评论(0编辑  收藏  举报