Create multiple directories

7stud bbxx789_05ss at yahoo.com
Mon Feb 25 03:07:15 CET 2008


On Feb 24, 6:27 pm, Jeff Schwab <j... at schwabcenter.com> wrote:
> Paul Lemelle wrote:
> > I am somewhat new to Python and I am trying to create a program that
> > automatically creates directories from a range of numbers. I
> > researched the os.mkdir & os.makedirs methods, but they do not seem to
> > (I don't know) how to include an argumnet to step through my list.
>
> > I woudl like to do the follwoing:
> > 1) Ask the user how many folders to create
> > 2) take raw_input and then translate it into a while loop that steps
> > through the os.mkdir process.
>
> Maybe something like this?
>
> import os
>
> def mkdirs(n):
>      for i in range(n):
>          os.mkdir("%d" % i)
>
> if __name__ == '__main__':
>      mkdirs(int(raw_input("How many folders should I create? ")))

Maybe something like this:

import os

num_str = raw_input("Enter number of dirs to create: ")
num = int(num_str)

for i in range(num):
    dir_name = "new_dir%s" % i  #same result as "new_dir" + str(i)
    os.mkdir(dir_name)  #creates dirs in current directory



More information about the Python-list mailing list