用python简单查找大文件

    

以前清扫用户文件夹时,总是要一个文件夹文件夹的找,今天有空就用python简单的写了个脚本,来查找给定大小范围的文件。

废话不说,上代码。

"""该脚本用于在用户目录中查找大的用户文件"""
import os,sys

def isLarge(file_path):
    """判断文件大小是否大于Bigjudge,大于返回文件大小,小于返回false"""
    Bigjudge = 200000000
    filesize = os.path.getsize(file_path)
    if os.path.isfile(file_path) == False:
        raise os.error("没有该文件")
    if(filesize > Bigjudge):
        return filesize
    else:
        return 0

def getAlltree(dir_path):
    """查找该目录下所有文件大小大于Bigjudge的文件,并打印。"""
    for name in os.listdir(dir_path):
        full_path = os.path.join(dir_path, name)
        if os.path.isfile(full_path):
            filesize=isLarge(full_path)            
            if filesize > 0:
                print("文件路径%s 文件大小%d" % (full_path,filesize))              
        if os.path.isdir(full_path):
            getAlltree(full_path)

    

def main():
    dir_path = r"H:\game time"    
    getAlltree(dir_path)
print("搜索结束orz")
if __name__ == '__main__': main()

结果如下:

  

原文地址:https://www.cnblogs.com/jonathanlife/p/2882239.html