Python reading whitespace-separated file lines as separate lines

后端 未结 6 794
野性不改
野性不改 2021-01-25 14:18

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

6条回答
  •  长情又很酷
    2021-01-25 15:08

    EDIT: This is the fix

    import os
    f = open("out.txt", "r")
    os.chdir("base location")
    for line in file:
        os.system("mkdir {}".format(line.strip().replace(" ", r"\ ")))
    

    The problem is that mkdir interprets its input as a list of directories to make, unless you escape the spaces.

提交回复
热议问题