By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

getting text from WinXP console

P: n/a
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris
Jul 18 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
You want the function raw_input().
Have you read the tutorial? I should have been covered there.

Jul 18 '05 #2

P: n/a
Chris Rebert (cybercobra) wrote:
You want the function raw_input().
Have you read the tutorial? I should have been covered there.


I'm pretty sure he's looking for a means of capturing
the entire contents of a console window, not just
letting the user enter some textual input.

To the OP: this is an OS-specific question, not a
Python question, and although you have a good chance
of getting an answer from someone here, your
chances are even higher if you ask in some Windows
newsgroup or mailing list. (But let us know when you
find the answer, in any case, for the archives.)

-Peter
Jul 18 '05 #3

P: n/a
On Sun, 20 Mar 2005 17:19:16 -0500, Peter Hansen <pe***@engcorp.com>
declaimed the following in comp.lang.python:
I'm pretty sure he's looking for a means of capturing
the entire contents of a console window, not just
letting the user enter some textual input.
Even worse -- He wants, as I read it, application-x to capture
the contents of a console window running application-y.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #4

P: n/a
Chris Maloof wrote:
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris


If you're looking to do this from Python then do "python <appname.py> >
output.txt" only without the quotes. AFAIK, this only works on Windows.
Correct me if it works from Linux or another OS.

--
--------------------------
Lucas Raab
lvraab located at earthlink.net
dotpyFE located at gmail.com
AIM: Phoenix11890
MSN: py*********@hotmail.com
IRC: lvraab
ICQ: 324767918
Yahoo: Phoenix11890
Jul 18 '05 #5

P: n/a
Lucas Raab wrote:
Chris Maloof wrote:
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris

If you're looking to do this from Python then do "python <appname.py> >
output.txt" only without the quotes. AFAIK, this only works on Windows.
Correct me if it works from Linux or another OS.


It works on Unix. :)
Jul 18 '05 #6

P: n/a
Jeff Schwab wrote:
Lucas Raab wrote:
Chris Maloof wrote:
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris


If you're looking to do this from Python then do "python <appname.py>
> output.txt" only without the quotes. AFAIK, this only works on

Windows. Correct me if it works from Linux or another OS.

It works on Unix. :)

Alright. Thanks, I wasn't sure.

--
--------------------------
Lucas Raab
lvraab located at earthlink.net
dotpyFE located at gmail.com
AIM: Phoenix11890
MSN: py*********@hotmail.com
IRC: lvraab
ICQ: 324767918
Yahoo: Phoenix11890
Jul 18 '05 #7

P: n/a
If you re-direct the output to a file, then you won't see it on the
console at all, unless the program writes to stderr separately.

Jul 18 '05 #8

P: n/a

Are you the one who is launching the Nethack program, or is it already
running, and you ar e trying to capture the text after Nethack is
launched ?

If you are launching it you can try using one of the python popen()
calls to redirect the screen output to your python program.

I havne't doen this myself, but I've seen this suggested to many people
who have asked a similar question.

Also- you can try posting a message to the Python Tutor- as well as
googling for python popen, to look for examples.
Another alternative, is to use the tee.exe unix util (ported to wn32)
which will duplicate screen I/O to a file, or some other place.

Try googling for "unix utilities win32" -or similar combinations of
these words.

Jul 18 '05 #9

P: n/a
Lucas Raab wrote:
Chris Maloof wrote:
Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).


If you're looking to do this from Python then do "python <appname.py> >
output.txt" only without the quotes. AFAIK, this only works on Windows.
Correct me if it works from Linux or another OS.


It works in *nix (and probably other OSes as well) to the same extent
that it works in Windows. However, it does *not* do what the OP was
asking for -- it captures the (console-mode) output of appname.py into
a file, but the OP (as I understand it) wanted to have appname.py
capture the already-displayed console output of someotherapp.exe.

Unless I'm seriously mistaken, the only way that this will be possible
is if there's a Win32 API call that will give the correct information.
This might be possible to find in the MSDN documentation, if it
exists, but I suspect that it probably doesn't.

Actually, there's probably a second way -- capture the window image as
a bitmap, and then run OCR software on it. I'm not sure how well most
OCR software will deal with ASCII graphics, though -- they tend to be
optimized for reading "real" text (i.e. words in an actual language).

If it's permissible for the capturing program to start the application
whose output is to be captured, then it may be possible to work as a
proxy -- the capture app would pass (almost) all input to the child
app and then retrieve the child app's output (and probably perform the
actual display on-screen). This won't let you capture the text of an
arbitrary window, though, and would probably be pretty fragile.

Jeff Shannon

Jul 18 '05 #10

P: n/a
Jeff Shannon wrote:
Unless I'm seriously mistaken, the only way that this will be possible
is if there's a Win32 API call that will give the correct information.
This might be possible to find in the MSDN documentation, if it exists,
but I suspect that it probably doesn't.


Just thinking: in NT, XP, and friends, the "DOS" console has
a scrollback buffer. In addition, even in 98 you can cut and
paste text in a console window. Both of these are clear
evidence that the data is available. Whether the API to get
to it is well-documented and available remains to be
answered... I'm guessing it is, somewhere...
Jul 18 '05 #11

P: n/a
Ron
On Mon, 21 Mar 2005 12:06:03 -0800, Jeff Shannon
<je*********@gmail.com> wrote:
Actually, there's probably a second way -- capture the window image as
a bitmap, and then run OCR software on it.


There's also OCR aware clipboard software that can cut a selected area
from the screen, convert the graphic image to text and put it on the
clipboard. They work fairly well.

Most of them aren't free. But a shareware version with a limited
trial period might work just fine for this one purpose.

Ron

Jul 18 '05 #12

P: n/a
Peter Hansen wrote:
Jeff Shannon wrote:
Unless I'm seriously mistaken, the only way that this will be possible
is if there's a Win32 API call that will give the correct information.
This might be possible to find in the MSDN documentation, if it
exists, but I suspect that it probably doesn't.


Just thinking: in NT, XP, and friends, the "DOS" console has
a scrollback buffer. In addition, even in 98 you can cut and
paste text in a console window. Both of these are clear
evidence that the data is available. Whether the API to get
to it is well-documented and available remains to be
answered... I'm guessing it is, somewhere...


Hm, that's a good point. It may even be possible to simulate
selecting the entire console window and copying the text to the
clipboard, and from there into the capturing application. It might be
worth exploring win32ui & friends...

Jeff Shannon

Jul 18 '05 #13

P: n/a
Thanks for the interesting suggestions!

On 21 Mar 2005 10:47:52 -0800, "Cappy2112" <ca*******@gmail.com>
wrote:
If you are launching it you can try using one of the python popen()
calls to redirect the screen output to your python program.
Aha. This sounds perfect, and I don't know why it doesn't work even
with a basic command window:
s = subprocess.Popen(args='c:\\WINDOWS\\system32\\comm and.com', stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) [[[completely blank command window appears]]] s.pid 372 s.stdin.write('hi there')
Traceback (most recent call last):
File "<pyshell#41>", line 1, in -toplevel-
s.stdin.write('hi there')
IOError: [Errno 22] Invalid argument s.stdout.read(1) '' s.stderr.read(1)

''

I'm guessing it's not the right kind of screen output, somehow? I
should try this with Unix.
Jeff Shannon <je*********@gmail.com> wrote:
Unless I'm seriously mistaken, the only way that this will be possible
is if there's a Win32 API call that will give the correct information.
This might be possible to find in the MSDN documentation, if it
exists, but I suspect that it probably doesn't.
I think you're right. I did consider the select-and-copy idea, which
sounds vaguely silly but acceptable if there's a way to do it without
getting the selection area highlighted. At this point I wouldn't be
terribly stunned if it's the best alternative, actually, though an API
call would seem nicer. I'll see if I can turn anything up in the MSDN
library. Peter's advice to ask about this on a Windows list is
probably wisest; I'll post again if I figure it out.

I refuse on principle to do a text -> image -> text conversion, clever
though it is. :)

Lucas Raab <do*****@gmail.com> wrote:If you're looking to do this from Python then do "python <appname.py> >
output.txt" only without the quotes. AFAIK, this only works on Windows.
Correct me if it works from Linux or another OS.


I do this all the time in Unix and had no idea it worked in Windows;
it's not quite what I need but still good to know!

Thanks to everybody for all the help.

Chris

Jul 18 '05 #14

P: n/a
>>Popen(args='c:\\WINDOWS\\system32\\command.com '

Don't launch command.com, try running the actual program you are
interested in in capturing the text for. Although you should be able to
run the command interpreter too- I don't know why that didn't work.

There are several variations of popen, so you may want to explore them
all to see which suits your needs.

Jul 18 '05 #15

P: n/a

"Chris Maloof" <cj******@gmail.com> wrote in message
news:ub********************************@4ax.com...
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris


Check out the Win32 Console functions on MSDN, such as AlocConsole(),
ReadConsoleOutput(), GetStdHandle(). I don't see an easy way to get the
screen buffer handle unless you are the process that owns the console,
however.

Mark
Jul 18 '05 #16

P: n/a
On Tue, 22 Mar 2005 13:11:59 -0800, "Mark T." <no****@nospam.com>
wrote:

"Chris Maloof" <cj******@gmail.com> wrote in message
news:ub********************************@4ax.com.. .
Hello,

Does anyone know how I can read the ASCII text from a console window
(from another application) in WinXP? It doesn't sound like a major
operation, but although I can find the window via pywin32, I haven't
been able to do anything with it. I'd really just like to get the
window text into a string.

By "console window", I mean the sort of thing that comes up when you
run "command" (although this particular one is for the game NetHack).

Thanks,
Chris


Check out the Win32 Console functions on MSDN, such as AlocConsole(),
ReadConsoleOutput(), GetStdHandle(). I don't see an easy way to get the
screen buffer handle unless you are the process that owns the console,
however.

Mark

Yup, this is pretty much closest to what ended up working, after some
more research. There might have been an easier way, but for the
archives, here's the meat of what I did (as seen also in the
python-win32 mailing list):

import win32process
from ctypes import *
import time

# Constants from winbase.h in .NET SDK
STD_INPUT_HANDLE = c_uint(-10) # (not used here)
STD_OUTPUT_HANDLE = c_uint(-11)
STD_ERROR_HANDLE = c_uint(-12) # (not used here)

startup = win32process.STARTUPINFO()
(hProcess, hThread, dwProcessId, dwThreadId) = \
win32process.CreateProcess("C:\myConsoleApp.exe",
"-X",
None,
None,
0,
0,
None,
None,
startup)
time.sleep(1) #wait for console to initialize

# Use ctypes to simulate a struct from the Win32 API
class COORD(Structure):
_fields_ = [("X", c_short),
("Y", c_short)]

topleft = COORD(0,0)

CHARS_TO_READ = 200
result = create_string_buffer(CHARS_TO_READ)
count = c_int()

windll.kernel32.AttachConsole(c_uint(dwProcessId))
inHandle = windll.kernel32.GetStdHandle(STD_INPUT_HANDLE)
outHandle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
errHandle = windll.kernel32.GetStdHandle(STD_ERROR_HANDLE)

# See Win32 API for definition
windll.kernel32.ReadConsoleOutputCharacterA(outHan dle,
result,
CHARS_TO_READ,
topleft,
byref(count))

print result.value
Jul 18 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.