new tool add prefix for directory
This commit is contained in:
commit
886bb38285
9
001.add_prefix_0_for_directory/readme.md
Normal file
9
001.add_prefix_0_for_directory/readme.md
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
# 脚本说明
|
||||||
|
该脚本会将当前目录下所有的文件夹名称加一个前缀0。
|
||||||
|
用于快速将 类似于 01.abc 02.cde 等文件夹名称变更为 001.abc 002.cde
|
||||||
|
|
||||||
|
# 使用方法
|
||||||
|
将该脚本拷贝到目标目录下
|
||||||
|
``` shell
|
||||||
|
python rename.py
|
||||||
|
```
|
||||||
21
001.add_prefix_0_for_directory/rename.py
Normal file
21
001.add_prefix_0_for_directory/rename.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
import os
|
||||||
|
|
||||||
|
def add_prefix_to_folders():
|
||||||
|
# 获取当前目录下的所有条目
|
||||||
|
for entry in os.listdir('.'):
|
||||||
|
# 检查是否为文件夹
|
||||||
|
if os.path.isdir(entry):
|
||||||
|
new_name = '0' + entry # 添加前缀0
|
||||||
|
|
||||||
|
# 避免覆盖已存在的目录
|
||||||
|
if not os.path.exists(new_name):
|
||||||
|
try:
|
||||||
|
os.rename(entry, new_name)
|
||||||
|
print(f"成功重命名: {entry} -> {new_name}")
|
||||||
|
except Exception as e:
|
||||||
|
print(f"错误:无法重命名 {entry}。原因:{str(e)}")
|
||||||
|
else:
|
||||||
|
print(f"警告:跳过 {entry},因为 {new_name} 已存在")
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
add_prefix_to_folders()
|
||||||
Loading…
Reference in New Issue
Block a user