如何从具有路径的列表创建文件夹树

时间:2019-04-17 06:43:54

标签: python-3.x operating-system

我试图将需要创建的文件夹结构的每个路径放入列表,然后使用os.makedirs()创建所有路径,但是出了点问题。仅创建头文件夹,而不创建子文件夹。

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)

尽管我打印了print(i)个正确的文件夹路径,但是像/home/usr/folder1/outputfolder一样仅创建了“ Head-Folderpaths”,而并非所有后续的文件夹路径都被创建。为什么这样?

这就是我得到的:

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

但这就是我所需要的:

/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

2 个答案:

答案 0 :(得分:1)

使用以下代码来保持逻辑和编码:

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)

您不创建文件夹。.您仅删除现有文件夹并重新创建(如果已存在)

我将添加其他内容以完成操作:

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)

答案 1 :(得分:0)

尝试一下(在我的Windows机器上测试过,但也可以在Linux上工作)

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