Ken
Ken

Reputation: 2335

Creating a pipe from the inside of the python interpreter

sometimes i just want to quickly redirect a large output to an external program, supposing that in Python 3.x i have

>>> import sys
>>> sys.modules.keys()

how i can redirect the output of

>>> sys.modules.keys()

to a specific command or application ?

Upvotes: 3

Views: 308

Answers (1)

glglgl
glglgl

Reputation: 91017

If you do such things quite often, it could be useful to create a helper module which essentially does

def pipeinto(data, *prog):
    import subprocess
    sp = subprocess.Popen(prog, stdin=subprocess.PIPE)
    sp.stdin.write(str(data))
    sp.stdin.close()
    return sp

which enables you to do

pipeinto("\n".join(sys.modules.keys()), "gedit")

Upvotes: 4

Related Questions