首页 > 其他分享 >删除指定日期前的文件

删除指定日期前的文件

时间:2023-03-09 16:15:02浏览次数:27  
标签:-% 00 删除 timestamp 指定 timeArray 日期 time dt

import os
import time


def TimeStampToTime(timestamp):
    timeStruct = time.localtime(timestamp)
    return time.strftime('%Y-%m-%d %H:%M:%S', timeStruct)
#


# dt = "2023-02-27 00:00:00"
dt = "2023-02-27"
# 转换成时间数组
# timeArray = time.strptime(dt, "%Y-%m-%d %H:%M:%S")
timeArray = time.strptime(dt, "%Y-%m-%d")
# 转换成时间戳
timestamp = time.mktime(timeArray)
# print(timestamp)
#
# print(t<timestamp)
# if t < timestamp:
#     print('<')

base_path = r'C:\Users\Administrator.DESKTOP-161KJQD\Desktop\aaa'

for root, dirs, files in os.walk(base_path):
    # print(root)
    # print(files)
    for f in files:
        fi = os.path.join(root, f)
        t = os.path.getmtime(fi)
        if t < timestamp:
            print('remove:', fi, TimeStampToTime(t))
            os.remove(fi)

 

标签:-%,00,删除,timestamp,指定,timeArray,日期,time,dt
From: https://www.cnblogs.com/moon3496694/p/17198833.html

相关文章