import os
class BatchRename():
def rename(self):
path = r"0915_ps_1/data_annotated_for_four"
filelist = os.listdir(path)
total_num = len(filelist)
i = 0
for item in filelist:
if item.endswith('.json'):
# 原文件路径
src = os.path.join(os.path.abspath(path), item)
# print(item) # str
print(src)
# 重命名1
# dst = os.path.join(os.path.abspath(path), '-2' + '.png')
# 完整路径到上级文件夹 不包括文件名
# print(os.path.abspath(path))
# 重命名2
dst = os.path.join(os.path.abspath(path), item.split(".")[0]+'-2' + '.json')
# print(item.split(".")[0]) # 文件名 不包含后缀
print(dst)
try:
os.rename(src, dst) # 原文件完整路径和目标文件完整路径 dst不能已存在
i += 1
except:
continue
print('total %d to rename & converted %d png' % (total_num, i))
if __name__ == '__main__':
demo = BatchRename()
demo.rename()
文件夹下批量重命名文件(可区别后缀)
于 2022-09-26 11:04:45 首次发布