Create multiple directories

Paul Rubin http
Sun Feb 24 21:15:57 EST 2008


7stud <bbxx789_05ss at yahoo.com> 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