首页 > 编程语言 >使用Python清理历史文件夹

使用Python清理历史文件夹

时间:2023-01-09 20:57:00浏览次数:43  
标签:name filePath Python delFlag 清理 文件夹 path os createTime

根据修改时间,删除文件夹。
例如:删除某个日期之前的文件夹。

# -*- coding: utf-8 -*-
# @Time    : 2023/1/9  19:34
# @Author  : King
# @Software: PyCharm
# @Describe: 
# -*- encoding:utf-8 -*-
import os
import time
import shutil

class Folder:
    def __init__(self, name, createTime, delFlag):
        self.name = name
        self.createTime = createTime
	self.delFlag = delFlag

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


def get_FileCreateTime(filePath):
    # '''获取文件的创建时间'''
    # filePath = unicode(filePath,'utf8')
    t = os.path.getctime(filePath)
    return TimeStampToTime(t)


def get_FileModifyTime(filePath):
    # '''获取文件的修改时间'''
    # filePath = unicode(filePath, 'utf8')
    t = os.path.getmtime(filePath)
    return TimeStampToTime(t)


def get_FileAccessTime(filePath):
    # '''获取文件的访问时间'''
    # filePath = unicode(filePath, 'utf8')
    t = os.path.getatime(filePath)
    return TimeStampToTime(t)


def get_FileSize(filePath):
    # '''获取文件的大小,结果保留两位小数,单位为MB'''
    # filePath = unicode(filePath,'utf8')
    fsize = os.path.getsize(filePath)
    fsize = fsize / float(1024 * 1024)
    return round(fsize, 2)

if __name__ == '__main__':
    list = []
    path = r"/home/hadoop/dmp/dmp/"
    currtime = "2022-12-31 00:00:00"
    for file_name in os.listdir(path):
        fullPath = os.path.join(path, file_name)
        createTime = get_FileCreateTime(fullPath)
        delFlag = createTime < currtime  #判断创建日期小于某个日期,即标记为删除
        f = Folder(fullPath, createTime, delFlag)
        if delFlag:
            list.append(f)

    for o in list:
        print(o.name, o.createTime, o.delFlag)
        #shutil.rmtree(o.name)   打开这行即可删除文件夹

标签:name,filePath,Python,delFlag,清理,文件夹,path,os,createTime
From: https://www.cnblogs.com/30go/p/17038490.html

相关文章

  • python数据分析与可视化【思维导图】
    python数据分析与可视化常用库numpy+matplotlib+pandas思维导图图中难免有错误,后期随着学习与应用的深入,会不断修改更新。当前版本号:1.0numpy介绍NumPy是什么?NumP......
  • Python笔记(3)——列表二:操作列表(Python编程:从入门到实践)
    一、遍历列表1. 遍历整个列表:使用for循环1colors=['red','yellow','blue','green']#定义列表2forcolorincolors:#使用循环:从列表中提取一个元素并将其存......
  • 004 python 程序运行日志使用方法
    导入包importlogging.handlersimportdatetimelogger=logging.getLogger("log")日志目录查找并创建ifos.path.isdir('log'):print("当前目录下存在log文......
  • 003 python一个整数或byte数据转为十六进制字符串不带0x
    把一个byte数据转化为字符,例如byte数据为05,要转换为十六进制字符串hexstr,不带0xd=5hs=((str(hex(d)))[2:]).zfill(2)如上,hs为转换后的字符串。原理就是先用hex转化......
  • Python
    一、Python特点:1、跨平台程序设计语言。2、解释型编程语言,没有编译环节。3、交互式编程语言(可以在命令提示符中编写代码)。4、面向对象编程语言。  二、搭建开发......
  • Python浅拷贝和深拷贝
    预备知识对象的三个属性python中每一个对象都有三个属性:唯一标志,类型和值。其中对象的标志是一串数字,是每个对象的唯一标识,位于同一地址的对象标志相同。对象的值相等和......
  • 用python处理html代码的转义与还原-转
    本篇博客来源:用python处理html代码的转义与还原&#x27;&amp;&lt;&gt;&quot;&#x27;等特殊字符摘要:html转义:importhtml#字符串转义str="<tag>aaa</tag>"str_out=......
  • node nodejs 绿色版 添加环境变量 淘宝镜像源 全局安装文件夹 npm_global npm_cache
    解压文件和创建两个文件夹配置一个文件将上面的两个文件夹路径添加到文件中同时添加npm的淘宝镜像源cache=D:\node-v16.17.0-win-x64\node_cacheprefix=D:\node-v......
  • Python经典开源项目
    Python-100-Days项目地址:https://github.com/jackfrued/Python-100-DaysPython-100-Days就是我上面说的“保姆级”教程,他的内容面面俱到包括了Python开发的方方面面,......
  • Socket爬虫:Python版
    简述:较为底层的爬虫实现,用于了解爬虫底层实现的具体流程,现在各种好用的爬虫库(如requests,httpx...等)都是基于此进行封装的。PS:本文只作为实现请求的代码记录,基础部分不做过......