Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

How can I get terminal output in python


>>> 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()
Comment

PREVIOUS NEXT
Code Example
Python :: numpy apply log to array 
Python :: Make A Snake Game Using Python and Pygame 
Python :: pandas filter rows by value in list 
Python :: How to Create a Pie Chart in Seaborn 
Python :: create sqlite database python 
Python :: python: check type and ifno of a data frame 
Python :: how to use enumerate instead of range and len 
Python :: getting image from path python 
Python :: python if not path exist make path 
Python :: how to make a crosshair in python 
Python :: django template datetime-local 
Python :: how to get height in pyqt5 
Python :: change column value based on another column pandas 
Python :: pandas new df from groupby 
Python :: scipy correlation 
Python :: Concatenate strings using Pandas groupby 
Python :: sort column with numeric and text data 
Python :: decrypt python code 
Python :: python order 2d array by secode element 
Python :: get all values of a dict python 
Python :: make longitude -180 to 180 
Python :: python clock 
Python :: activate venv enviroment 
Python :: python turtle shooting game 
Python :: UnavailableInvalidChannel error in conda 
Python :: how to import tkinter in python 
Python :: how to convert an image to matrix in python 
Python :: how to append element python 
Python :: how to keep columns in pandas 
Python :: libreoffice add row at the end of table 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =