python实现文件夹们倒换
程序员文章站
2022-06-10 11:13:25
...
原先的路径格式是ABCDE文件夹每个下面都有12345文件夹,现在改成12345文件夹下每个都有ABCDE文件夹。
import os
import shutil
def copyfile(srcfile, dstfile):
if not os.path.isfile(srcfile):
print("%s not exist!" % srcfile)
else:
fpath, fname = os.path.split(dstfile) # 分离文件名和路径
if not os.path.exists(fpath):
os.makedirs(fpath) # 创建路径
shutil.copyfile(srcfile, dstfile) # 复制文件
print("copy %s -> %s" % (srcfile, dstfile))
directorys=os.listdir("./")
files={}
for directory in directorys:
if os.path.isdir( directory):
layers={}
fs=os.listdir(os.path.join("./",directory))
for f in fs:
if os.path.isdir(os.path.join("./",directory,f)):
layers[f]=os.listdir(os.path.join("./",directory,f))
files[directory]=layers
for size_info in files:
for layer in files[size_info]:
for file in os.listdir(os.path.join("./",size_info,layer)):
copyfile(os.path.join("./",size_info,layer,file),
os.path.join("F:/ji_chi_zhong",layer,"ji_chi_zhong",size_info,file))