winnt win32process.createProcess with stdout to file ?

  • Thread starter david.humpherys
  • Start date
D

david.humpherys

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)
 
P

Peter Hansen

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
 
R

Roger Upole

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_WRITE)
Also, you shouldn't have FILE_FLAG_OVERLAPPED set if you're
not passing an overlapped object into CreateFile.

hth
Roger
 
D

david.humpherys

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_WRITE, 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
 
R

Roger Upole

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
 
D

david.humpherys

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)
 
R

Roger Upole

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top