athreee commited on
Commit
4cc441e
·
verified ·
1 Parent(s): d857a81

Upload rename_txt_matching_media.py

Browse files
Files changed (1) hide show
  1. rename_txt_matching_media.py +28 -0
rename_txt_matching_media.py ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+
3
+ # 遍历文件夹及其子文件夹
4
+ def rename_txt_files_in_folder(folder_path):
5
+ # 遍历文件夹内所有文件
6
+ for root, dirs, files in os.walk(folder_path):
7
+ # 获取所有图片、视频、gif文件,包括webm, zip等
8
+ media_files = [f for f in files if f.lower().endswith(('webm', 'zip', 'jpg', 'webp', 'mp4', 'png', 'gif'))]
9
+
10
+ for media_file in media_files:
11
+ # 找到图片、视频或gif对应的txt文件
12
+ txt_file = media_file + '.txt'
13
+ media_file_path = os.path.join(root, media_file)
14
+ txt_file_path = os.path.join(root, txt_file)
15
+
16
+ # 如果txt文件存在,并且命名不正确
17
+ if os.path.exists(txt_file_path):
18
+ # 计算正确的txt文件路径
19
+ correct_txt_file_path = os.path.join(root, media_file.split('.')[0] + '.txt')
20
+
21
+ # 如果txt文件名错误,进行重命名
22
+ if txt_file_path != correct_txt_file_path:
23
+ os.rename(txt_file_path, correct_txt_file_path)
24
+ print(f"Renamed: {txt_file_path} -> {correct_txt_file_path}")
25
+
26
+ # 调用函数,传入你想处理的文件夹路径
27
+ folder_path = r"F:\新建文件夹"
28
+ rename_txt_files_in_folder(folder_path)