简体   繁体   中英

How to merge many folders in one folder in python?

I need to merge many folders in one folder with respecting their order which based on time, For that I have tried:

import os
import shutil
DestFolder =  'C:\\Users\\user\\Folder1'
SrcFolder1  = 'C:\\Users\\user\\Folder2'
for root, dirs, files in os.walk((os.path.normpath(SrcFolder1)), topdown=True):
    for name in files:
        SourceFolder = os.path.join(root,name)
        shutil.copy2(SourceFolder, DestFolder) 

I have three folders: SrcFolder1,SrcFolder1, DestFolder

SrcFolder1 contains:

 Test1_Key=90_Msg=87.npy
 Test2_Key=90_Msg=54.npy
 Test3_Key=90_Msg=b8.npy

SrcFolder2 contains:

 Test1_Key=90_Msg=5f.npy
 Test2_Key=90_Msg=87.npy
 Test3_Key=90_Msg=ab.npy

I have executed my code with the first SrcFolder1 , then when the second SrcFolder2, the resulted DestFolder is:

 Test1_Key=90_Msg=87.npy
 Test1_Key=90_Msg=5f.npy
 Test2_Key=90_Msg=54.npy
 Test2_Key=90_Msg=87.npy
 Test3_Key=90_Msg=b8.npy
 Test3_Key=90_Msg=ab.npy

However, I need that mys DestFolder is organised like this:

 Test1_Key=90_Msg=87.npy
 Test2_Key=90_Msg=54.npy
 Test3_Key=90_Msg=b8.npy
 Test1_Key=90_Msg=5f.npy
 Test2_Key=90_Msg=87.npy
 Test3_Key=90_Msg=ab.npy

If permission is denied then try running as administrator with this code:

import admin
if not admin.isUserAdmin():
    admin.runAsAdmin()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM