Please help me understand how subprocess.popen works
How to get output from subprocess.Popen along with visible execution?
Using python with subprocess Popen - Stack Overflow
How to use subprocess popen Python - Stack Overflow
Videos
To at least really start the subprocess, you have to tell the Popen-object to really communicate.
def run_command(command):
p = subprocess.Popen(command, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return p.communicate()
You can look into Pexpect, a module specifically designed for interacting with shell-based programs.
For example launching a scp command and waiting for password prompt you do:
child = pexpect.spawn('scp foo [email protected]:.')
child.expect ('Password:')
child.sendline (mypassword)
See Pexpect-u for a Python 3 version.
subprocess.Popen takes a list of arguments:
from subprocess import Popen, PIPE
process = Popen(['swfdump', '/tmp/filename.swf', '-d'], stdout=PIPE, stderr=PIPE)
stdout, stderr = process.communicate()
There's even a section of the documentation devoted to helping users migrate from os.popen to subprocess.
In the recent Python version, subprocess has a big change. It offers a brand-new class Popen to handle os.popen1|2|3|4.
The new subprocess.Popen()
import subprocess
subprocess.Popen('ls -la', shell=True)
Its arguments:
subprocess.Popen(args,
bufsize=0,
executable=None,
stdin=None, stdout=None, stderr=None,
preexec_fn=None, close_fds=False,
shell=False,
cwd=None, env=None,
universal_newlines=False,
startupinfo=None,
creationflags=0)
Simply put, the new Popen includes all the features which were split into 4 separate old popen.
The old popen:
Method Arguments
popen stdout
popen2 stdin, stdout
popen3 stdin, stdout, stderr
popen4 stdin, stdout and stderr
You could get more information in Stack Abuse - Robert Robinson. Thank him for his devotion.
The link I found: https://www.tutorialspoint.com/python/os_popen.htm, tells me that os.popen works like a pipe, but my book gives me a very confusing example:
1) open('something.py')
which is because we need an open stream. next it gives:
h = os.popen('type something.py').read()At this point, I'm not getting, if os.popen() acts like a pipe('|' on the terminal), then how is this supposed to be pipeing to the stream opened. and what is it piping?