python 批量重命名
记录下批量重命名的代码。
主要运用了os模块的listdir和rename函数。
import os # 导入os模块
from tqdm import tqdm # tqdm进度条模块
train_path = 'input/train/'
mask_path = 'input/train_masks/'
train_ids = os.listdir(train_path)
mask_ids = os.listdir(mask_path)
print(train_ids)
print(mask_ids)
for i in tqdm(range(len(train_ids)),total=len(train_ids)):
old_train_name = train_path + train_ids[i] #旧文件名称(加地址)
new_train_name = train_path +str(i+1) + '.jpg' #新文件名称(加地址)
old_mask_name = mask_path + mask_ids[i]
new_mask_name = mask_path + str(i + 1) + '.jpg'
os.rename(old_train_name,new_train_name)
os.rename(old_mask_name, new_mask_name)
print('Done')
转载自:https://blog.csdn.net/u014005417/article/details/86646163