How do I pipe the output of file to a variable in Python?
Question: 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?


Answer: 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]
    

