How to create folder tree from list with paths

I am trying to put every path of a folder structure I need to be created into a list and then create all of them with os.makedirs() but something goes wrong. Only the Head-Folders are created, not the Sub-Folders.

def output_folders(trcpaths):
    #trcpath is a list with several paths, example: ['/home/usr/folder1', '/home/usr/folder2']
    global outputfolders
    outputfolders = []
    #Create Paths
    for x, j in enumerate(trcpaths):
        for i in os.listdir(trcpaths[x]):
            if i.endswith('trc'):
                folderpath1 = (j + '/' + i).split('.')[0] #/home/usr/folder1/outputfolder
                folderpath2 = folderpath1 + '/Steps' #/home/usr/folder1/outputfolder/Steps
                folderpath3 = folderpath2 + '/Step_1' #/home/usr/folder1/outputfolder/Steps/Step_1
                folderpath4 = folderpath2 + '/Step_2'
                folderpath5 = folderpath2 + '/Step_3'
                folderpath6 = folderpath2 + '/Step_4'
                folderpath7 = folderpath2 + '/Threshold'

                outputfolders.append(folderpath1)
                outputfolders.append(folderpath2)
                outputfolders.append(folderpath3)
                outputfolders.append(folderpath4)
                outputfolders.append(folderpath5)
                outputfolders.append(folderpath6)
                outputfolders.append(folderpath7)

    #Create Folders
    for j, i in enumerate(outputfolders):
        print(i)
        if os.path.exists(i):
            if j == 0:
                input('The Output-Folder already exists! Overwrite?' )
            shutil.rmtree(i)
            os.makedirs(i)

Although when I print(i) the right folderpaths are printed but only the "Head-Folderpaths" are created like /home/usr/folder1/outputfolder and not all subsequent folderpaths. Why so?

This is what I get:

/home/usr/folder1/outputfolder
/home/usr/folder2/outputfolder

But this is what I need:

/home/usr/folder1/outputfolder
/home/usr/folder1/outputfolder/Steps
/home/usr/folder1/outputfolder/Steps/Step_1
/home/usr/folder1/outputfolder/Steps/Step_2
/home/usr/folder1/outputfolder/Steps/Step_3
/home/usr/folder1/outputfolder/Steps/Step_4
/home/usr/folder1/outputfolder/Steps/Threshold
/home/usr/folder2/outputfolder
/home/usr/folder2/outputfolder/Steps
/home/usr/folder2/outputfolder/Steps/Step_1
/home/usr/folder2/outputfolder/Steps/Step_2
/home/usr/folder2/outputfolder/Steps/Step_3
/home/usr/folder2/outputfolder/Steps/Step_4
/home/usr/folder2/outputfolder/Steps/Threshold

Upvotes: 1

Views: 1050

Answers (2)

Frenchy
Frenchy

Reputation: 17007

to keep your logic and your coding, with this code:

for j, i in enumerate(outputfolders):
    print(i)
    if os.path.exists(i):
        if j == 0:
            input('The Output-Folder already exists! Overwrite?' )
        shutil.rmtree(i)
        os.makedirs(i)

you dont create folder..you only delete the existing folder and recreate if it already exists

i'll add else to complete the operation:

for j, i in enumerate(outputfolders):
    print(i)
    if os.path.exists(i):
        if j == 0:
            input('The Output-Folder already exists! Overwrite?' )
        shutil.rmtree(i)
        os.makedirs(i)
    else:
        os.makedirs(i)

Upvotes: 1

balderman
balderman

Reputation: 23815

Try this (Tested on my Windows machine but should work on Linux as well)

import os

NUM_OF_STEPS = 5


def make_output_folders(trc_paths):
    output_folders = []
    for idx, path in enumerate(trc_paths):
        for leaf in os.listdir(path):
            if leaf.endswith('trc') and os.path.isdir(os.path.join(path, leaf)):
                trc_folder = os.path.join(path, leaf)
                output_folders.append(os.path.join(trc_folder, 'output_folder', 'Steps'))
                steps_folder = output_folders[-1]
                for x in range(1, NUM_OF_STEPS):
                    output_folders.append(os.path.join(steps_folder, 'Step_{}'.format(x)))
                output_folders.append(os.path.join(trc_folder,'output_folder', 'Threshold'))

        for _path in output_folders:
            print(_path)
            if not os.path.exists(_path):
                os.makedirs(_path)
        output_folders = []


# 'folder_1' contains a sub folder named '1_trc' 
# 'folder_2' contains a sub folder named '2_trc'
make_output_folders(['c:\\temp\\55721430\\folder1', 'c:\\temp\\55721430\\folder2'])

Upvotes: 0

Related Questions