stdout custom

castironpi at gmail.com castironpi at gmail.com
Wed Mar 19 03:38:00 EDT 2008


> >> > Can I allocate a second console window, so I can place certain output
> >> > to that directly, and leave the original streams alone?  I tried
> >> Have you tried using the creationflags argument to subprocess.Popen?
> >> Specially the CREATE_NEW_CONSOLE flag. See the Microsoft documentation
> >> for CreateProcess  
> >> athttp://msdn2.microsoft.com/en-us/library/ms682425(VS.85).aspx
> >> (Note that a process can be attached at most to one console)
>
> > One console per process is fine, but I tried using 'cmd.exe',
> > 'cmd.exe /K', and 'more.com' (fully specified in c/windows/system32)
> > as separate processes.  The sign is the console window splashes up and
> > vanishes right away.
>
> Apparently you have to either redirect stdout AND stdin, or none of them.  
> This worked for me:
>
> p = subprocess.Popen('c:\\windows\\system32\\cmd.exe',
>          stdout=subprocess.PIPE, stdin=subprocess.PIPE,
>          creationflags=subprocess.CREATE_NEW_CONSOLE)
> p.communicate("dir\n")

This worked for me.

import subprocess
p = subprocess.Popen('c:\\windows\\system32\\cmd.exe',
         stdout=subprocess.PIPE, stdin=subprocess.PIPE,
         creationflags=subprocess.CREATE_NEW_CONSOLE)

import time
import threading
def th():
	while 1:
		time.sleep( .01 )
		s= p.stdout.read(1)
		print( ':', end= '' )
		if not s: continue
		print( s.decode(), end= '' )


import thread
thread.start_new_thread( th, () )
time.sleep( 2 )
p.stdin.write( b'c:\\windows\\system32\\more.com\n' )
p.stdin.flush()
print ( 'abc' )
while 1:
	time.sleep( 1 )
	p.stdin.write( b'abc\n' )
	p.stdin.flush()
	print ( 'abc' )
p.wait()
time.sleep( 10 )



More information about the Python-list mailing list