|
@@ -0,0 +1,46 @@
|
|
|
|
+import os
|
|
|
|
+import re
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+def rename_files(folder_path):
|
|
|
|
+ # 遍历指定文件夹及其子文件夹
|
|
|
|
+ for foldername, subfolders, filenames in os.walk(folder_path):
|
|
|
|
+ for filename in filenames:
|
|
|
|
+ # 构建完整的文件路径
|
|
|
|
+ file_path = os.path.join(foldername, filename)
|
|
|
|
+
|
|
|
|
+ # 获取文件名(不含路径)
|
|
|
|
+ base_name = os.path.basename(file_path)
|
|
|
|
+
|
|
|
|
+ # 使用正则表达式保留文件名中的小数点,删除开头的数字
|
|
|
|
+ new_name = re.sub(r'^\d+', '', base_name, count=1)
|
|
|
|
+
|
|
|
|
+ # 构建新的文件路径
|
|
|
|
+ new_path = os.path.join(foldername, new_name)
|
|
|
|
+
|
|
|
|
+ # 避免文件名冲突,添加后缀
|
|
|
|
+ counter = 1
|
|
|
|
+ while os.path.exists(new_path):
|
|
|
|
+ # 文件名已存在,添加后缀
|
|
|
|
+ new_name = re.sub(r'^\d+', '', base_name, count=1) + ('_' + str(counter) if counter > 1 else '')
|
|
|
|
+ new_path = os.path.join(foldername, new_name)
|
|
|
|
+ counter += 1
|
|
|
|
+
|
|
|
|
+ # 重命名文件
|
|
|
|
+ os.rename(file_path, new_path)
|
|
|
|
+ print(f'Renamed: {file_path} -> {new_path}')
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+# 指定要修改文件名的文件夹路径
|
|
|
|
+folder_path = '/Users/admin/Downloads/市文旅体局'
|
|
|
|
+
|
|
|
|
+# 调用函数
|
|
|
|
+rename_files(folder_path)
|
|
|
|
+
|
|
|
|
+# folder_path = '/Users/admin/Downloads/市文旅体局'
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|