文件替换:保留原始路径

文件替换:保留原始路径

问题描述:将特定类型文件的文件挑出来,放入一个文件夹,批量处理之后再替换原文件。

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
# xyz_a.py
# %%
import os
import shutil

from hashlib import md5


# 创建 md5 对象
def encrypt_md5(s):
new_md5 = md5()
new_md5.update(s.encode(encoding='utf-8'))
return new_md5.hexdigest()


# old_root = os.getcwd() # 待处理文件的根目录
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}')

# shutil.copyfile(old_dir, 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
# xyz_b.py
# %%
import os
import pandas as pd

import shutil


# ------
df = 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:
# print(filename)
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)

# f1, f2 = os.path.split(old_dir)[0], os.path.split(old_dir)[1]
# print(f1, f2)

# shutil.copyfile(new_dir, old_dir)

except Exception as result:
print(result)