[Tutor] subprocess & pyw conflict ?

Luke Paireepinart rabidpoobear at gmail.com
Thu Dec 7 01:31:43 CET 2006


Dave S wrote:
> Hi all,
>
> I thought I had my solution with subprocess ... my test code ...
>
>
>
> #!/usr/bin/env python
> # -*- coding: iso8859_1 -*- 
>
> import subprocess
>         
> a = subprocess.Popen('tasklist.exe', bufsize=0, stdout=subprocess.PIPE, 
> universal_newlines=True)
> op = a.stdout.readlines()
>
> for i in op:
>     if i.split(' ')[0] == 'gmanager.exe':
>         f = open('E:\Documents and Settings\All 
> Users\Desktop\gsr_running', 'w')
>         f.close()
>             
>
>
> works a treat when I run it as proc.py detects the process I am looking for & 
> writes a dummy file to the desktop. :) but I get a black windows terminal 
> flash up.
>
> The code will eventually run in an app.pyw so to check it would be OK I 
> renamed my working proc.py to proc.pyw - it fails :(, No window (as 
> expected), no dummy file (not expected) - the process gmanager.exe is 
> running.
>
> So there seems to be a problem with subprocess & pyw
>
> Googling I found ...
> https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1358527&group_id=5470
> So I tried the suggested workaround with proc.pyw ...
>
>
>
> #!/usr/bin/env python
> # -*- coding: iso8859_1 -*- 
>
> import subprocess
>     
> a = subprocess.Popen('tasklist.exe', bufsize=0, stdin=subprocess.PIPE, 
> stdout=subprocess.PIPE, universal_newlines=True)
> op = a.stdout.readlines()
> a.stdin.close()
>
> for i in op:
>     if i.split(' ')[0] == 'gmanager.exe':
>         f = open('E:\Documents and Settings\All 
> Users\Desktop\gsr_running', 'w')
>         f.close()
>         
>
> Still zip, and because there is no terminal, I cannot view any errors !
>
> Any suggestions welcome :)
>   
Well, because I'm batting 0 on this thread so far, I think I'll just go 
ahead and suggest another bad solution!
You could try redirecting the output of sys.stderr to a file, and you 
might be able to see the error message! :D

If you hadn't said 'any suggestions welcome' I might've kept this to 
myself :P

 >>> import sys
 >>> class TestClass(object):
    def __init__(self):
        self.data = []
    def write(self,item):
        self.data.append(item)
 >>> a = TestClass()
 >>> sys.stderr = a
 >>> salfjdsljfka321423
 >>> print a.data
['\nTraceback (most recent call last):', '\n', '  File "<pyshell#14>", 
line 1, in -toplevel-\n', '    salfjdsljfka321423\n', "NameError: name 
'salfjdsljfka321423' is not defined\n"]

Except instead of a file-like class, you could just use a real file.  
But then it would only leave the last line intact.
So you'd probably want to make a class that wraps a file object, where 
the write method just appends to an internal list,
and it writes it all out to the file when you call the Class.close() or 
whatever.
Actually, I guess the program stops executing on an exception...
Hmm, not really sure what you'd do exactly.


Sure, there are better solutions, and this doesn't really help you with 
your original problem, but it at least lets you see your error message!
HTH,
-Luke
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   



More information about the Tutor mailing list