user4609262
user4609262

Reputation:

Make Multiple Subdirectories In Python

I am doing a kind of special code right here. I am making a code that will create 1 - 10 directories called box1, box2... until 10. In these directories there will be other directories. And in these directories more subdirectories... . At the end, it will generate a key. Here is what I have for now:

import time
import random
import subprocess
import os
import os.path

liste = []
old = []
key = False
numberOfBoxes = 5
files = -1
number = random.randint(1, 10)

def random_generator():
    for looper in range(1, numberOfBoxes):
        rand = random.randint(1, 10)
        liste.append(rand)


def calculate():
    c = liste[0]
    for looper in range(1, c):
        print(looper)
        newpath = 'Box' + str(looper)
        if not os.path.isdir('./' +newpath+'/'):
            os.mkdir('./' +newpath+'/')


def subdir_true():
    files = -1
    read = liste[0]
    chance = random.randint(1, 2)
    for looper in range(1, read):
        if chance == 1:
            for looper in range(1, read):
                exist = os.path.exists('Box' + str(looper))
                if exist == True: 
                    newpath = 'Box' + str(looper)
                    old.append(newpath)
                    files += 1
                    oldF = old[files]
                    number = random.randint(1, 10)
                    for looper in range(1, number):
                        newpath = 'Box' + str(looper)
                        os.mkdir('./'+oldF+'/'+newpath+'/')


random_generator()
calculate()
subdir_true()

How do I make subdirectories into subdirectories using for looper in range()?

Upvotes: 0

Views: 83

Answers (1)

Steven Mercatante
Steven Mercatante

Reputation: 25305

See the docs for os.makedirs()

Recursive directory creation function. Like mkdir(), but makes all intermediate-level directories needed to contain the leaf directory. Raises an error exception if the leaf directory already exists or cannot be created. The default mode is 0777 (octal). On some systems, mode is ignored. Where it is used, the current umask value is first masked out.

Upvotes: 2

Related Questions