Python's subprocess module returning different results from Unix shell

后端 未结 4 1155
灰色年华
灰色年华 2021-01-13 01:34

I\'m trying to get a list of the CSV files in a directory with python. This is really easy within unix:

ls -l *.csv

And, predictably, I get

4条回答
  •  遥遥无期
    2021-01-13 02:36

    p=subprocess.Popen(["ls", "-l", "*.out"], stdout = subprocess.PIPE, shell=True)
    

    causes

    /bin/sh -c ls -l *.out
    

    to be executed.

    If you try this command in a directory, you'll see -- in typical mystifying-shell fashion -- all files are listed. And the -l flag is ignored as well. That's a clue.

    You see, the -c flag is picking up only the ls. The rest of the arguments are being eaten up by /bin/sh, not by ls.

    To get this command to work right at the terminal, you have to type

    /bin/sh -c "ls -l *.out"
    

    Now /bin/sh sees the full command "ls -l *.out" as the argument to the -c flag.

    So to get this to work out right using subprocess.Popen, you are best off just passing the command as a single string

    p=subprocess.Popen("ls -l *.out", stdout = subprocess.PIPE, shell=True)
    output,error=p.communicate()
    print(output)
    

提交回复
热议问题