import os
import shutil
"""
将每个文件夹下的两个不同文件夹内容分别保存在2个文件夹内
文件夹格式:
|-1
|-1
|-2
|-2
|-1
|-2
|-3
|-1
|-2
"""
def conbine_file(old_folder ,new_folder1 ,new_folder2 ,lastDir1 ,lastDir2):
filenames = os.listdir(old_folder)
if not os.path.exists(new_folder1):
os.mkdir(new_folder1)
if not os.path.exists(new_folder2):
os.mkdir(new_folder2)
for file in filenames:
filepath = old_folder + "\\" + file
for files in os.listdir(filepath):
if files==lastDir1:
target_folder = filepath + "\\" + files
for i in os.listdir(target_folder):
shutil.move(target_folder + "\\" + i, new_folder1 + "\\" + i)
print(target_folder + "\\" + i,"=====", new_folder1 + "\\" + i)
if files==lastDir2:
target_folder = filepath + "\\" +files
for i in os.listdir(target_folder):
shutil.move(target_folder + "\\" + i, new_folder2 + "\\" + i)
print(target_folder + "\\" + i, "=====", new_folder2 + "\\" + i)
if __name__ == '__main__':
old_folder = r"./1"
new_folder1 = r"E:\17-20\2021-10\3"
new_folder2 = r"E:\17-20\2021-10\4"
lastDir1 = "1"
lastDir2 = "2"
conbine_file(old_folder ,new_folder1 ,new_folder2 ,lastDir1 ,lastDir2)