Execute a system command: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: `communicate()` is a safer method in general than `stdout.read()`, `stdin.write()`, etc)
Line 366: Line 366:


{{works with|Python|2.4 (and above)}}
{{works with|Python|2.4 (and above)}}
import subprocess
from subprocess import PIPE, Popen, STDOUT
output = subprocess.Popen('ls', shell=True, stdout=subprocess.PIPE).stdout
p = Popen('ls', stdout=PIPE, stderr=STDOUT)
print output.read()
print p.communicate()[0]


'''Note:''' The latter is the preferred method for calling external processes, although cumbersome, it gives you finer control over the process.
'''Note:''' The latter is the preferred method for calling external processes, although cumbersome, it gives you finer control over the process.