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

winnt win32process.createProcess with stdout to file ?

P: n/a
os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.

dh
--------------------------------------------------------------------------
import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE, 0,
sa, win32file.OPEN_EXISTING, win32file.FILE_FLAG_SEQUENTIAL_SCAN |
win32file.FILE_FLAG_OVERLAPPED , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, None, None, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)

Jul 18 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
da*************@gmail.com wrote:
os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.


Use the standard "subprocess" module that comes in Python2.4.

Or, if you can't upgrade for some reason, it sounds to me
like os.popen() will do what you want... that's the older
standard technique.

No need to resort to lots of win32 stuff.

-Peter
Jul 18 '05 #2

P: n/a
You'll need to pass security attributes with inherit=True
to CreateProcess also, and the file has to be opened with sharing.
(win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WR ITE)
Also, you shouldn't have FILE_FLAG_OVERLAPPED set if you're
not passing an overlapped object into CreateFile.

hth
Roger
<da*************@gmail.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
os:winnt
python2.3.2

I have a exe that dumps info to the command line. I want to run this
process and capture the stdout into a file. I think i'm close... any
help appreciated.

dh
--------------------------------------------------------------------------
import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE, 0,
sa, win32file.OPEN_EXISTING, win32file.FILE_FLAG_SEQUENTIAL_SCAN |
win32file.FILE_FLAG_OVERLAPPED , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, None, None, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 18 '05 #3

P: n/a
Roger, I updated the script (below).. but now I get errors...
many thanks for your help.

import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WRI TE, sa,
win32file.OPEN_ALWAYS, win32file.FILE_FLAG_SEQUENTIAL_SCAN , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)
ERROR:
pywintypes.error:(2, 'createProcess', ' the system cannot find the file
specified.;)

it's strange that it gets this error even though i have the
win32file.OPEN_ALWAYS flag set..

any suggestions... (soo... close!! )
thanks, thanks!
david

Jul 18 '05 #4

P: n/a
If it got past the CreateFile call, the problem's not with the log file.
This error from CreateProcess means it can't find your executable.

Roger

<da*************@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Roger, I updated the script (below).. but now I get errors...
many thanks for your help.

import win32process, win32file, win32security, win32con, win32api,
thread, win32event, win32pipe

cmd = "c:/myexe.exe"
sa = win32security.SECURITY_ATTRIBUTES()
sa.bInheritHandle = 1

startInfo = win32process.STARTUPINFO()
startInfo.dwFlags = win32process.STARTF_USESTDHANDLES

fh = win32file.CreateFile("c:/mylog.log", win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ|win32file.FILE_SHARE_WRI TE, sa,
win32file.OPEN_ALWAYS, win32file.FILE_FLAG_SEQUENTIAL_SCAN , 0)
startInfo.hStdOutput = fh
startInfo.hStdError = win32api.GetStdHandle(win32api.STD_ERROR_HANDLE)
startInfo.hStdInput = win32api.GetStdHandle(win32api.STD_INPUT_HANDLE)

hProcess, hThread, dwProcessId, dwThreadId = win32process.CreateProcess
\
( None, cmd, sa, sa, 1, win32con.NORMAL_PRIORITY_CLASS, None, None,
startInfo)
ERROR:
pywintypes.error:(2, 'createProcess', ' the system cannot find the file
specified.;)

it's strange that it gets this error even though i have the
win32file.OPEN_ALWAYS flag set..

any suggestions... (soo... close!! )
thanks, thanks!
david


----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 18 '05 #5

P: n/a
Aweseome! Many many thanks Roger ! You've made my day.

The last thing that you may be able to help with...
I'm using
win32api.TerminateProcess(hProcess,3)
to kill this process if it gets outta hand...
but when i do so.. it seems that the stdout/stderr don't capture the
output.

here's my last chunk of code.
again.. thank you so much.

hProcess, hThread, dwProcessId, dwThreadId =
win32process.CreateProcess \
( None, self.RENDER_STRING, sa, sa, 1,
win32con.NORMAL_PRIORITY_CLASS, None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.TerminateProcess(hProcess,3)

self.exitCode = win32process.GetExitCodeProcess(hProcess)
time.sleep(2)

Jul 18 '05 #6

P: n/a
TerminateProcess doesn't give it a chance to exit normally
and do any cleanup that would happen if it exited itself.
It may not have been able to flush its file buffers, etc.
Does the executable have any way to signal it to exit ?

Roger
<da*************@gmail.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Aweseome! Many many thanks Roger ! You've made my day.

The last thing that you may be able to help with...
I'm using
win32api.TerminateProcess(hProcess,3)
to kill this process if it gets outta hand...
but when i do so.. it seems that the stdout/stderr don't capture the
output.

here's my last chunk of code.
again.. thank you so much.

hProcess, hThread, dwProcessId, dwThreadId =
win32process.CreateProcess \
( None, self.RENDER_STRING, sa, sa, 1,
win32con.NORMAL_PRIORITY_CLASS, None, None, startInfo)

while self.exitCode == 259:
if self.stop:
print "kill da process"
win32api.TerminateProcess(hProcess,3)

self.exitCode = win32process.GetExitCodeProcess(hProcess)
time.sleep(2)



----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= East/West-Coast Server Farms - Total Privacy via Encryption =---
Jul 18 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.