This is on Windows Server 2008 R2. I have a file of inputs, one input per line. Some of the inputs have spaces in them. I\'m trying to use the below simple code, but it separate
The other answers here are all right -- the problem has to do with how Unix commands work -- if you want to use mkdir to create a directory with a name containing spaces, you have to either escape the spaces or put quotes around the name so that your Unix environment knows there is only one argument, and it has spaces in it; otherwise, it will make one directory per argument/word.
I just want to add 2 things:
Whenever you open()
a file, you need to make sure you close()
it. The easiest way to do this is to use a context manager, like with open(file) as f:
.
os
actually has a mkdir
function that takes a string as an argument. You could just pass each line to os.mkdir
as-is.
So, you could revise your code like this:
import os
with open("out.txt", "r") as f:
os.chdir("base location")
for line in f:
os.mkdir(line.strip())