[Tutor] XP & catching execl o/p ?

Dave S pythontut at pusspaws.net
Wed Dec 6 00:02:23 CET 2006


On Tuesday 05 December 2006 22:42, Luke Paireepinart wrote:
> Dave S wrote:
> > On Tuesday 05 December 2006 20:58, Luke Paireepinart wrote:
> >> Dave S wrote:
> >>> Hi all,
> >>>
> >>> Struggling with python & XP again. My app needs to know if a certain
> >>> program is running on my XP box
> >>>
> >>> Ideal world - I can get the output of 'tasklist.exe' into a string.
> >>>
> >>> I have tried
> >>>
> >>> os.execl('....')
> >>> It throws the output to the terminal + I need the exact path to the
> >>> executable (a bit of a trial)
> >>>
> >>> and
> >>> os.startfile('...')
> >>> it throws the output to a different window, but no exact path needed
> >>>
> >>> Any ideas how I can catch the output ?
> >>
> >> You could try redirecting sys.stdout to a file-like class instance you
> >> create.
> >
> > OK ... could you give me a bit more on that ? ... probably me being a bit
> > dense :)
>
> Um...
>
> #test.py
> class FileLikeObject(object):
>     def __init__(self,current_stdout):
>         self.data = []
>         self.oldstdout = current_stdout
>
>     def write(self,arg):
>         if arg != '\n':
>             self.data.append(arg)
>
>     def output(self):
>         sys.stdout = self.oldstdout
>         print self.data
>         sys.stdout = self
>
>
>
> import sys
>
> f = FileLikeObject(sys.stdout)
> sys.stdout = f
>
> print "hello."
> f.output()
> print "hi"
> f.output()
> #-----------
>
> Be careful playing with the dark magic....
> Don't forget that print goes to stdout.
> HTH,
> -Luke

Indeed it is dark :)

Thanks for that - I will play

Dave






>
> > Cheers
> >
> > Dave
> >
> >>> Cheers
> >>>
> >>> Dave
> >>>
> >>>
> >>> _______________________________________________
> >>> Tutor maillist  -  Tutor at python.org
> >>> http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list