reading content from a cmd window via python
Question: I'm trying to connect to an existing cmd window and read its content.

It is an arbitrary cmd window and not a child process.

Any ideas how this can be done with python?

Thanks in advance, Omer.


Answer: ** **Note: the[original version of the
question](http://stackoverflow.com/revisions/7567680/1) asked how to read AND
write to a cmd window** **

**Writing**

You can write to an existing command window with code such as:

    
    
    from pywinauto import application
    
    app = application.Application()
    
    app.connect_(path= r"C:\WINDOWS\system32\cmd.exe")
    dlg = app.top_window_()
    dlg.TypeKeys('hello world')
    

Notes:

  1. I installed the latest version of pywinauto into a Python 2.6 installation direct from the Mercurial repository with the command:

`pip install -e hg+https://code.google.com/p/pywinauto/#egg=pywinauto`

  2. I would make this rather more robust than assuming the path to cmd.exe! Documentation on selecting the application is at <http://pywinauto.googlecode.com/hg/pywinauto/docs/HowTo.html>

**Reading**

Reading from an existing command window appears to be somewhat more difficult!
Someone on the [pywinauto-users mailing
list](https://lists.sourceforge.net/lists/listinfo/pywinauto-users) has got it
working & is offering to post a working example:
<http://thread.gmane.org/gmane.comp.python.pywinauto.user/249/focus=252> I
suggest you get in touch with him.

