文件替换:保留原始路径 问题描述:将特定类型文件的文件挑出来,放入一个文件夹,批量处理之后再替换原文件。
1. 文件迁移
将文件路径经过md5加密后,作为文件名前缀。
复制文件到一个新文件夹
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 import osimport shutilfrom hashlib import md5def encrypt_md5 (s ): new_md5 = md5() new_md5.update(s.encode(encoding='utf-8' )) return new_md5.hexdigest() old_root = "E:\\" new_root = "D:\\0\\" exts = ['.xyz' ] for parent, dirnames, filenames in os.walk(old_root): for filename in filenames: extension = os.path.splitext(filename)[1 ] if extension.lower() in ext: old_dir = os.path.join(parent, filename) new_name = encrypt_md5(old_dir) + '_' + filename new_dir = os.path.join(new_root, new_name) print (f'{old_dir} ;; {new_name} ;; {new_dir} ' )
用命令行运行:1 python xyz_a.py > zzz.csv
2. 文件处理 略
3. 替换原始文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import osimport pandas as pdimport shutildf = pd.read_csv('zzz.csv' , encoding='utf16' , sep=';;' , ) df.columns = ['old_dir' , 'new_name' , 'new_dir' ] df['new_name' ] = df['new_name' ].str .strip() print (df.head())new_root = "D:\\0\\" for parent, dirnames, filenames in os.walk(new_root): for filename in filenames: new_dir = os.path.join(parent, filename) rst = df[df['new_name' ] == filename] try : old_dir = rst['old_dir' ].values[0 ] print (new_dir, '->' , old_dir) except Exception as result: print (result)