[Python] 纯文本查看 复制代码
import os
def movedir(top_dir):
for dir_name in os.listdir(top_dir):
# 过滤文件夹名,包含四道杠
if dir_name.find("-") != -1:
name_arr = dir_name.split("-")
item_num = len(name_arr)
if item_num == 5:
base_dir = os.path.join(top_dir, dir_name) # 在已有的文件夹里面创建
for i in range(3, item_num+1):
new_dir_name = "-".join(name_arr[:i])
base_dir = os.path.join(base_dir, new_dir_name)
if not os.path.exists(base_dir):
os.makedirs(base_dir)
movedir(r"D:\projects\testproject\test")
效果:
├─2219-007-180098-JG-001
│ └─2219-007-180098
│ └─2219-007-180098-JG
│ └─2219-007-180098-JG-001
├─2219-007-180098-K-002
│ └─2219-007-180098
│ └─2219-007-180098-K
│ └─2219-007-180098-K-002
└─2219-007-180098-Y-005
└─2219-007-180098
└─2219-007-180098-Y
└─2219-007-180098-Y-005 |