Why does simple echo in subprocess not working

前端 未结 2 1384
名媛妹妹
名媛妹妹 2020-12-17 19:52

I\'m trying to perform simple echo operation using subprocess:

import subprocess
import shlex

cmd = \'echo $HOME\'
proc = subprocess.Popen(shlex.split(cmd),         


        
2条回答
  •  粉色の甜心
    2020-12-17 20:16

    You are confusing the two different invocations of Popen. Either of these will work:

    proc=subprocess.Popen(['/bin/echo', 'hello', 'world'], shell=False, stdout=subprocess.PIPE)
    

    or

    proc=subprocess.Popen('echo hello world', shell=True, stdout=subprocess.PIPE)
    

    When passing shell=True, the first argument is a string--the shell command line. When not using the shell, the first argument is a list. Both produce this:

    print proc.communicate()
    ('hello world\n', None)
    

提交回复
热议问题