7

I need to create a list of folders each with multiple subfolders that are not nested. I have used

os.makedirs('folder/subfolder')

to create a folder and a subfolder but I can only make multiple nested folders work:

os.makedirs('folder/subfolder1/subfolder2/subfolder3')

where sub3 is nested in sub2 which is nested in sub1. What I want is for sub 1, 2 and 3 all to be nested in 'folder' together (3 folders inside 1 folder). I tried

os.makedirs('folder/(subfolder1, subfolder2)')

but that just creates a folder titled "(subfolder1, subfolder2)" Does anyone know the correct syntax for this? Is it even possible with the makedirs function?

3 Answers 3

16

Use a loop:

for i in range(1,100):
    os.makedirs(os.path.join('folder', 'subfolder' + str(i)))

Or, if you have the names in a list:

subfolder_names = []
for subfolder_name in subfolder_names:
    os.makedirs(os.path.join('folder', subfolder_name))

p.s.

In case to ignore already-exist folder

os.makedirs('/path/to/dir', exist_ok=True)
Sign up to request clarification or add additional context in comments.

4 Comments

Why not subfolders and subfolder?
I think it's more descriptive (Just subfolder could be anything; a handle, an ID etc.). Anyway, it's just a variable name!
Awesome, this works. I think I need two loops to create each folder and then create the list of subfolders within each folder.
How can I create another "level" of subfolders? I mean, I'd like to create a structure like this one: folder/(subfolder_1, subfolder_2, subfolder_3) where the subfolder between parenthesis are on the same second level, and then subfolder_1/(subfolder_1.1, subfolder_1.2), subfolder_2/(subfolder_2.1, subfolder_2.2), subfolder_3/(subfolder_3.1, subfolder_3.2)?
2

You can't do it in one call like that. Just put one call after another:

os.makedirs("folder/subfolder1")
os.makedir("folder/subfolder2")

2 Comments

Darn. I have to create subs in about 100 folders. I was hoping it would be quicker.
You can use a loop to make it faster. for subfolder in subfolders: os.makedir(os.path.join("folder", subfolder))
2

You can loop using a list comprehension, create the directory at each iteration using os.mkdir and assigning it a name that is the result of joining the base path to a given directory name.

import os

[os.mkdir(os.path.join("/folder", "subdir{}".format(i))) for i in range(100)]

6 Comments

Could you explain it a bit, please?
@dawid-ferenczy Sure. It's a list comprehension python-3-patterns-idioms-test.readthedocs.org/en/latest/… with two nested methods from the os library.
No, sorry, I meant if you can edit your answer and add some explanation there (a little bit better than "It's a list comprehension"). It's not for me, rather for preventing the answer deletion in Low Quality Posts review and also for others. Stack Overflow users shouldn't just copy&paste answers, but rather understand a problem and be able to solve it themselves. But that means they need some explanation.
I understand and i'm sorry but the system doesn't allow me to edit the answer
Yep another user left his editing form open probably. Now i could edit.
|

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.