Create multiple directories

Paul Rubin http
Mon Feb 25 03:15:57 CET 2008

7stud <bbxx789_05ss at> writes:
> 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.

More information about the Python-list mailing list