Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pipe the output of file to a variable in Python?

Tags:

python

How do I pipe the output of file to a variable in Python?

Is it possible? Say to pipe the output of netstat to a variable x in Python?

like image 659
Jake Avatar asked Jun 30 '10 21:06

Jake


2 Answers

It is possible. See:

http://docs.python.org/library/subprocess.html#replacing-bin-sh-shell-backquote

In Python 2.4 and above:

from subprocess import *
x = Popen(["netstat", "-x", "-y", "-z"], stdout=PIPE).communicate()[0]
like image 129
Richard Fearn Avatar answered Nov 15 '22 03:11

Richard Fearn


Two parts:

Shell

netstat | python read_netstat.py

Python read_netstat.py

import sys
variable = sys.stdin.read()

That will read the output from netstat into a variable.

like image 39
S.Lott Avatar answered Nov 15 '22 02:11

S.Lott