Python扫描某个磁盘中所有的照片路径

一、Python扫描某个磁盘中所有的照片

代码如下(示例):

import os
S=0  #查询文件的总大小
fileCount=0 #查询的文件数目
dirCount=0 #查询的文件夹数目
targetCount=0 #目标文件数
Except='' #查询时的异常路径存放
def search(path):
   list=os.listdir(path)
   for i in range(len(list)):
    try:
          Path=path+list[i]
          if(os.path.isdir(Path)):
            global dirCount
            dirCount+=1
            Path+="\\"
            search(Path)
          else:
            global fileCount,S,targetCount
            fileCount+=1
            S+=os.path.getsize(Path)
            if(Path.endswith('.jpg') or Path.endswith('.png')):
                if(os.path.getsize(Path)/1024>1024):
                  targetCount+=1
                  print(Path+"     "+str(int(os.path.getsize(Path)/1024))+"K")
    except:
        global Except
        Except+='拒绝访问->'+Path+'\n'

search('E:\\')#------->这里修改访问路径


猜你喜欢

转载自blog.csdn.net/weixin_46466198/article/details/113729574