3

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 ?

5
  • 1
    like? give us an example? and by the way, use ipython instead not the python interpreter itself, your life would be much easier Commented Oct 22, 2012 at 12:42
  • @Ali this is a pipe cal | gedit i want to do something like this sys.modules.keys() | gedit Commented Oct 22, 2012 at 12:44
  • @Ali I am also looking for a viable solution for applications that offer a built-in python interpreter and i can't use iPython. Commented Oct 22, 2012 at 12:49
  • @Ken the newer versions of iPython comes with a "backend" server that iPython interfaces with and it has exposed api to communicate with in your apps, does that help, if now, why can't you use iPython? Commented Oct 22, 2012 at 14:05
  • @Ali simply because i have to use the python interpreter built-in in the application. Many applications these days offers a built-in Python interpreter and i can't just use 1 random python interpreter. Commented Oct 22, 2012 at 14:07

1 Answer 1

4

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")
Sign up to request clarification or add additional context in comments.

2 Comments

no built-in solution ? For example i find useful the ! solution for the command line ftp client in linux which executes local commands .
@Ken: glglgl's answer using the subprocess module is, I think, the best generic solution you'll find. Any other approach is going to be specific to some IDE you could use to edit your Python programs. The standard interpreter doesn't have an ! command or anything. It only does Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.