2

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.

2
  • 2
    you need to update directory in the loop Commented Apr 20, 2017 at 19:32
  • @MooingRawr Wow that was surprisingly simple.Thanks a lot! Commented Apr 20, 2017 at 19:39

1 Answer 1

4
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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.