Answers for "how to get the terminal command's result in pyton"

3

how to get the terminal command's result in pyton

>>> import subprocess
>>> cmd = [ 'echo', 'arg1', 'arg2' ]
>>> output = subprocess.Popen( cmd, stdout=subprocess.PIPE ).communicate()[0]
>>> print output
arg1 arg2

>>> 

There is a bug in using of the subprocess.PIPE. For the huge output use this:

import subprocess
import tempfile

with tempfile.TemporaryFile() as tempf:
    proc = subprocess.Popen(['echo', 'a', 'b'], stdout=tempf)
    proc.wait()
    tempf.seek(0)
    print tempf.read()
Posted by: Guest on October-15-2020
1

how to get the terminal command's result in pyton

import os
os.system("command")
Posted by: Guest on October-09-2021

Code answers related to "how to get the terminal command's result in pyton"

Python Answers by Framework

Browse Popular Code Answers by Language