迁移脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
import os  
from shutil import copyfile

source_dir = "/Users/maqi/SecondBrain/"
target_dir = "/Users/maqi/Public/blog/temp/"
dirs = os.listdir(source_dir)
for cur_dir in dirs:
if not cur_dir.__contains__('.'):
cur_path = os.path.join(source_dir, cur_dir)
files = os.listdir(cur_path)
for file in files:
if not files.__contains__('.'):
copyfile(os.path.join(cur_path, file), os.path.join(target_dir, file))

批量重命名

1
2
3
4
5
6
7
8
9
10
11
12
import os  

source_dir = "/Users/maqi/SecondBrain/机器学习&深度学习的副本"
target_dir = "/Users/maqi/SecondBrain/机器学习&深度学习的副本"
target_categories = "categories: 机器学习&深度学习\n"
files = os.listdir(source_dir)
for file in files:
with open(os.path.join(source_dir, file), 'r') as f:
content = f.readlines()
content[3] = target_categories
with open(os.path.join(source_dir, file), 'w') as f:
f.writelines(content)