# filedeal.py #!/usr/bin/env python # -*- coding:utf-8 -*- import os import shutil from PIL import Image import io import requests import datetime # 获取所有文件 def getAllFiles(fire_dir): filepath_list = [] for root,folder_names,file_names in os.walk(fire_dir): for file_name in file_names: file_path = root+os.sep+file_name filepath_list.append(file_path) print(file_path) print(filepath_list) return filepath_list # 获取图片的像素 def getPicsize(pic_file): pic_file =pic_file img = Image.open(pic_file) w = img.width h =img.height geshi = img.format image_size = os.path.getsize(pic_file) print(image_size) print(w) print(h) return w,h,image_size def getBaseName(file_name): file_base_name = os.path.basename(file_name) return file_base_name def getNewName(old_file_name): file_base_name = os.path.basename(old_file_name) timestrhaomiao = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f_') new_file_name = old_file_name.split(file_base_name)[0]+timestrhaomiao+file_base_name print(new_file_name) return new_file_name def deal_file(src,dst1,dst2,dst3): # 区分jpg和mp4 mp4 = [] jpg = [] png = [] jpeg = [] qita=[] #先给所有文件重命名 filepath_list = getAllFiles(src) for f in filepath_list: old_file_name =f new_file_name = getNewName(old_file_name) os.rename(old_file_name,new_file_name) #然后再次获取所有文件内容 filepath_list = getAllFiles(src) for f in filepath_list: print(f) # for f in os.listdir(src): #根据具体需求更改后缀识别参数(.mp4和jpg等) if f.endswith('.mp4'): mp4.append(f) elif f.endswith('.jpg'): jpg.append(f) elif f.endswith('.png'): png.append(f) elif f.endswith('.jpeg'): jpeg.append(f) else: qita.append(f) # 创建目标文件夹 if not os.path.isdir(dst1): os.mkdir(dst1) dst2_list = [dst2, dst2 + "\\横图", dst2 + "\\横图\\大于等于1M", dst2 + "\\横图\\小于1M", dst2 + "\\竖图", dst2 + "\\竖图\\大于等于1M", dst2 + "\\竖图\\小于1M" ] for one_dst2 in dst2_list: if not os.path.isdir(one_dst2): os.mkdir(one_dst2) if not os.path.isdir(dst3): os.mkdir(dst3) # 拷贝文件到目标文件夹 for m in mp4: try: _mp4 = os.path.join(src,m) shutil.move(_mp4,dst1) except Exception as e: print(e) for j in jpg: try: _jpg = os.path.join(src,j) w,h,image_size = getPicsize(pic_file=_jpg) if w>h: if image_size < 1024000: shutil.move(_jpg,dst2+"\\横图\\小于1M") else: shutil.move(_jpg, dst2 + "\\横图\\大于等于1M") else: if image_size < 1024000: shutil.move(_jpg, dst2 + "\\竖图\\小于1M") else: shutil.move(_jpg, dst2 + "\\竖图\\大于等于1M") except Exception as e: print(e) for p in png: try: _png = os.path.join(src,p) w,h,image_size = getPicsize(pic_file=_png) if w>h: if image_size < 1024000: shutil.move(_png,dst2+"\\横图\\小于1M") else: shutil.move(_png, dst2 + "\\横图\\大于等于1M") else: if image_size < 1024000: shutil.move(_png, dst2 + "\\竖图\\小于1M") else: shutil.move(_png, dst2 + "\\竖图\\大于等于1M") except Exception as e: print(e) for jp in jpeg: try: _jpeg = os.path.join(src,jp) w,h,image_size = getPicsize(pic_file=_jpeg) if w>h: if image_size < 1024000: shutil.move(_jpeg,dst2+"\\横图\\小于1M") else: shutil.move(_jpeg, dst2 + "\\横图\\大于等于1M") else: if image_size < 1024000: shutil.move(_jpeg, dst2 + "\\竖图\\小于1M") else: shutil.move(_jpeg, dst2 + "\\竖图\\大于等于1M") except Exception as e: print(e) for q in qita: try: _qita = os.path.join(src,q) shutil.move(_qita,dst3) except Exception as e: print(e) #获取简化的新名字 def getJiHuaNewName(old_file_name,diji=0): file_base_name = os.path.basename(old_file_name) timestrhaomiao = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f_') #获取文件名字的后缀 houzui = file_base_name.split(".")[-1] print("huozui:") print(houzui) new_file_name = old_file_name.split(file_base_name)[0]+timestrhaomiao+str(diji)+"."+houzui print(new_file_name) return new_file_name #只给文件重新命名,名字简短化 def get_jiahua_mingzi(src): #先给所有文件重命名 filepath_list = getAllFiles(src) filepath_list_len = len(filepath_list) for i in range(0,filepath_list_len): old_file_name =filepath_list[i] new_file_name = getJiHuaNewName(old_file_name,i) os.rename(old_file_name,new_file_name) if __name__ == "__main__": src = r"E:\englishpic\picture\图片\横图" # file = r"F:\存储盘\其他\people\image\picture\xibi专场" # src = os.path.join(file, '混合') get_jiahua_mingzi(src)
标签:重新命名,name,python,1M,dst2,简短,file,path,os From: https://www.cnblogs.com/jingzaixin/p/17232669.html