batch mkdir using a file list

D

DataSmash

Hello,
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))

Appreciate any help you can give!
R.D. Harles
 
F

Fredrik Lundh

DataSmash said:
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>
 
J

Jeremy Jones

DataSmash said:
Hello,
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))

Appreciate any help you can give!
R.D. Harles
Untested code:

import os
for line in open("list.txt", "r"):
os.mkdir(line)


- JMJ
 
M

mensanator

Jeremy said:
Untested code:

import os
for line in open("list.txt", "r"):
os.mkdir(line)

That won't work (in Windows at least) because you have
to remove the "\n" from the line before you pass it to
os.mkdir.
 
D

DataSmash

I am using bash shell on windows and I'm getting the error:
TypeError: loop over non-sequence

Is there any way around not messing with the text file.
I want to batch generate the text lists as well.

Thanks!
 
D

DataSmash

OR...if python can't handle this type of text file,
what do I need to do to remove the "\n" from the file?
 
P

Peter Otten

DataSmash said:
I am using bash shell on windows and I'm getting the error:
TypeError: loop over non-sequence

Use open(filename).readlines() instead of open(filename) or switch to a
current Python version.
OR...if python can't handle this type of text file,
what do I need to do to remove the "\n" from the file?

line.strip() removes all leading and trailing whitespace (not just "\n")
from the line string.

import os
for line in open("list.txt").readlines():
directory = line.strip()
os.mkdir(directory)

Peter
 
D

DataSmash

Awesome! That worked!
Much thanks to Peter and all of you who took the time to answer my
question.
R.D.
 

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

Similar Threads

os.walk/list 3
Using Batch files 0
Need help with this script 4
Defining features in a list 5
Translater + module + tkinter 1
is there a better way to mkdir? 13
More List Comparison Help 1
Batch commands on Windows 12

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top