Thursday, 15 May 2014

python - Why does simple echo in subprocess not working -


I am trying to do simple echo operations using subprocess:

  import Copy subprocess import chain Cmd = 'echo $ HOME' proc = subprocess.Popen (shlex.split (cmd), shell = true, stdout = subprocess.PIPE) print proc.communicate () [0]   

But it does not print anything. Even if I change the "code" command to "Hello, World" in echo, it does not even print anything. Any help is appreciated.

On Unix, shell = true means that 2 and the following argument shell , Use a string to pass the command in a shell:

  import subprocess cmd = 'resonant $ HOME' proc = subprocess.open (cmd, shell = true, stdout = Subprocess.PIPE) print proc.communicate () [0],   

You can also write to:

  import subproduct cmd = 'echo $ HOME 'print subprocess.check_output (cmd, shell = true),   

to:

with shell = True , If the algorithms are a string in the shell default / bin / sh, then the string specifies the command to run through the shell. This means that the string should be formatted properly, because it will be typed at the shell prompt. This includes, for example, in filenames, with the exception of avoiding empty spaces with them or backslash if Args is a sequence, then the first item specifies the command string, and any extra items in addition to the shell This means that Popen is equivalent to:

  Popen (['/ bin / sh', '-c', args [ 0], args [1], ...])    

No comments:

Post a Comment