0

My code doesn't work, the intention was to create folders within folders.

import os

path = "D:\DOCS\Desktop"
n_folders = 100
x = 0
y = 0



os.chdir(path)
os.mkdir("Initial_Folder")

Second_Layer = []

while x < len(Second_Layer):
    while y < int(n_folders)+1:
        os.chdir(path + "\Initial_Folder")
        os.mkdir("\SecondLayer" + str(x))
        Second_Layer.append(x)
        y = y + 1
os.chdir(path + "\Initial_Folder"+"\SecondLayer" + str(x))
os.mkdir("\ThirdLayer" + str(y))
x = x + 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Don't put a backslash at the beginning of `SecondLayer`. That makes it an absoluete pathname, so it tries to create it in a root folder, not the current folder. – Barmar Aug 28 '23 at 21:47
  • You can use [`os.makedirs()`](https://docs.python.org/3/library/os.html#os.makedirs) to create all the directories at once. – Barmar Aug 28 '23 at 21:48
  • 1
    Get in the habit of using raw strings when you write Windows pathnames. See https://stackoverflow.com/questions/2953834/how-should-i-write-a-windows-path-in-a-python-string-literal – Barmar Aug 28 '23 at 21:48

0 Answers0