[Python] 清理下载文件夹源码

2333 2
晓凤科技实名认证 2023-2-18 18:12:07 | 显示全部楼层 |阅读模式
import os 
import threading 
import time 

def get_file_list(file_path): 
#文件按最后修改时间排序 
    dir_list = os.listdir(file_path) 
    if not dir_list: 
        return 
    else: 
        dir_list = sorted(dir_list, key=lambda x: os.path.getmtime(os.path.join(file_path, x))) 
    return dir_list 

def get_size(file_path): 
    """[summary] 
    Args: 
        file_path ([type]): [目录] 

    Returns: 
        [type]: 返回目录大小,MB 
    """ 
    totalsize=0 
    for filename in os.listdir(file_path): 
        totalsize=totalsize+os.path.getsize(os.path.join(file_path, filename)) 
    #print(totalsize / 1024 / 1024) 
    return totalsize / 1024 / 1024 

def detect_file_size(file_path, size_Max, size_Del): 
    """[summary] 
    Args: 
        file_path ([type]): [文件目录] 
        size_Max ([type]): [文件夹最大大小] 
        size_Del ([type]): [超过size_Max时要删除的大小] 
    """ 
    print(get_size(file_path)) 
    if get_size(file_path) > size_Max: 
        fileList = get_file_list(file_path) 
        for i in range(len(fileList)): 
            if get_size(file_path) > (size_Max - size_Del): 
                print ("del :%d %s" % (i + 1, fileList[i])) 
                #os.remove(file_path + fileList[i]) 
晓凤科技实名认证 2023-2-25 07:19:51 | 显示全部楼层
为什么有bug了
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

晓凤科技

初级红客

关注
  • 31
    主题
  • 11
    粉丝
  • 0
    关注
我是胡锦程,中国晓凤网络安全的创始人,会python等编程语言,懂得kali linux的使用,我时刻准备着,为中国网络安全事业作出贡献。

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行