File size: 1,344 Bytes
4cc441e |
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 |
import os
# 遍历文件夹及其子文件夹
def rename_txt_files_in_folder(folder_path):
# 遍历文件夹内所有文件
for root, dirs, files in os.walk(folder_path):
# 获取所有图片、视频、gif文件,包括webm, zip等
media_files = [f for f in files if f.lower().endswith(('webm', 'zip', 'jpg', 'webp', 'mp4', 'png', 'gif'))]
for media_file in media_files:
# 找到图片、视频或gif对应的txt文件
txt_file = media_file + '.txt'
media_file_path = os.path.join(root, media_file)
txt_file_path = os.path.join(root, txt_file)
# 如果txt文件存在,并且命名不正确
if os.path.exists(txt_file_path):
# 计算正确的txt文件路径
correct_txt_file_path = os.path.join(root, media_file.split('.')[0] + '.txt')
# 如果txt文件名错误,进行重命名
if txt_file_path != correct_txt_file_path:
os.rename(txt_file_path, correct_txt_file_path)
print(f"Renamed: {txt_file_path} -> {correct_txt_file_path}")
# 调用函数,传入你想处理的文件夹路径
folder_path = r"F:\新建文件夹"
rename_txt_files_in_folder(folder_path)
|