stdout custom

castironpi at gmail.com castironpi at gmail.com
Tue Mar 18 06:09:08 EDT 2008


On Mar 17, 8:16 pm, Gabriel Genellina <gagsl-... at yahoo.com.ar> wrote:
> On 17 mar, 19:43, castiro... at gmail.com wrote:
>
> > Can I allocate a second console window, so I can place certain output
> > to that directly, and leave the original streams alone?  I tried some
> > things in subprocess (Py 3a3 /WinXP) but they failed.  I don't know if
> > it's supposed to be possible though, so I didn't press very hard or
> > keep the code.  If it is, I can go repro where it went south.  Is it?
>
> 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)
>
> If your goal is to output some debug information, try using
> OutputDebugString + the DebugView utility fromwww.sysinternals.com

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.

>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdout= subprocess.P
IPE, creationflags= subprocess.CREATE_NEW_CONSOLE )
>>> p.poll()
0
>>> p= subprocess.Popen( 'c:\\windows\\system32\\cmd.exe', stdout= subprocess.PI
PE, creationflags= subprocess.CREATE_NEW_CONSOLE )
>>> p.poll()
0
>>> p= subprocess.Popen( 'c:\\windows\\system32\\cmd.exe /K', stdout= subprocess
.PIPE, creationflags= subprocess.CREATE_NEW_CONSOLE )
>>> p.poll()
0
>>> f= open( 'temp.txt', 'a' )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\cmd.exe /K', stdout= f, creatio
nflags= subprocess.CREATE_NEW_CONSOLE )
>>> p.poll()
0

------------------------Couple other symptoms.

>>> f.write( b'abc' )
'f.write' is not recognized as an internal or external command,
operable program or batch file.

>>>
f.write( b'abc' )
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Programs\Python\lib\io.py", line 1240, in write
    s.__class__.__name__)
TypeError: can't write bytes to text stream
>>>
f.write( 'abc' )
3
>>>

>>> ^Z

>>>

>>> ^Z
^Z



5000
09871234

------------------------
>>> f.write('2'*2000)
2000
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> f= os.fdopen( q[0], 'a' )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> f.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Programs\Python\lib\io.py", line 1378, in read
    res += decoder.decode(self.buffer.read(), True)
  File "C:\Programs\Python\lib\io.py", line 564, in read
    self._unsupported("read")
  File "C:\Programs\Python\lib\io.py", line 240, in _unsupported
    (self.__class__.__name__, name))
io.UnsupportedOperation: BufferedWriter.read() not supported
>>> f.read(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Programs\Python\lib\io.py", line 1384, in read
    readahead, pending = self._read_chunk()
  File "C:\Programs\Python\lib\io.py", line 1277, in _read_chunk
    readahead = self.buffer.read1(self._CHUNK_SIZE)
AttributeError: 'BufferedWriter' object has no attribute 'read1'
>>> f.write()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: write() takes exactly 2 positional arguments (1 given)
>>> f.write('2')
1
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags= 16 )
>>> p= subprocess.Popen( 'c:\\windows\\system32\\more.com', stdin= f, creationfl
ags=0 )
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
22222222222222222222222222222222222222222222222222222222222222222222222222222222
-------------------------

Writing my own process is an option.  I did not try sysinternals yet.



More information about the Python-list mailing list