batch mkdir using a file list

Fredrik Lundh fredrik at pythonware.com
Fri Sep 23 20:41:51 CEST 2005


DataSmash <rdh at new.rr.com> wrote

> I think I've tried everything now and can't figure out how to do it.
> I want to read in a text list from the current directory,
> and for each line in the list, make a system directory for that name.
>
> My text file would look something like this:
> 1144
> 1145
> 1146
> 1147
>
> I simply want to create these 4 directories.
> It seems like something like the following
> code should work, but it doesn't.
>
> import os
>
> file = open("list.txt", "r")
> read = file.read()
> print "Creating directory " + str(read)
> os.mkdir(str(read))

read() returns *all* text in the file as a single string, but you
really want to process each line for itself.  try this:

    for name in open("list.txt"):
        name = name.strip() # get rid of extra whitespace
        os.mkdir(name)

</F> 






More information about the Python-list mailing list