user7897789
user7897789

Reputation:

How can I create multiple directories using a loop in python?

I want to create 10 directories with a loop and I tried this code:

import os
pathname = 1
directory = "C:\Directory\Path\Name\\" + str(pathname)


while pathname < 11:
    if not os.path.exists(directory):
        os.makedirs(directory)
    pathname += 1  

But it is only creating the first directory and stopping as if it's not even going through the rest of the loop.I'm fairly new to python and this code made sense to me and I don't know why it might not work.Any help is appreciated.

Upvotes: 2

Views: 2387

Answers (1)

J&#39;e
J&#39;e

Reputation: 3746

import os
pathname = 1
directory = "C:\Directory\Path\Name\\" + str(pathname)

while pathname < 11:
    if not os.path.exists(directory):
        os.makedirs(directory)
    pathname += 1  
    directory = "C:\Directory\Path\Name\\" + str(pathname)

Upvotes: 4

Related Questions