jonathanlife

用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()

结果如下:

  

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2021-11-18
  • 2021-11-18
  • 2021-07-21
  • 2021-10-19
  • 2021-12-10
  • 2021-09-09
  • 2022-12-23
猜你喜欢
  • 2021-10-12
  • 2021-09-22
  • 2021-06-28
  • 2021-12-15
相关资源
相似解决方案