1

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
4
  • What is the exact python version that you are using? I am asking since os.makedirs() behaviour is quite version dependent. See docs.python.org/3/library/os.html#os.makedirs Commented Apr 17, 2019 at 6:50
  • Python 3.6.7, I edited it in the post Commented Apr 17, 2019 at 6:52
  • What is trcfilename - is it a folder? Commented Apr 17, 2019 at 7:01
  • Yes. It is a folder that has the same name as a file. I am analyzing trc-files and want the outputfolder to have the same name as the input file. Sorry for the confusion. I changed it into /outputfolder Commented Apr 17, 2019 at 7:02

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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'])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.