Create multiple directories

P

Paul Lemelle

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.

Any would help would be appreicated.

Paul
 
J

Jeff Schwab

Paul said:
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? ")))
 
7

7stud

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
 
P

Paul Rubin

7stud said:
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

I find it's useful to keep all the filenames the same length and
put leading zeros in the directory numbers, so that sorting their
names lexicographically puts them in numerical order. Untested:

fmt = "new_dir%0%d" % len(str(num))
for i in xrange(num):
os.mkdir(fmt % i)

Or if num is unknown but you have some upper bound like 1000,
you could just use %05d or something like that.
 
P

Paul Lemelle

7stud & Jeff,

Thanks for yoru input - there's still a few things for me to learn. :)


Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top