Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Dave S
On Wednesday 06 December 2006 11:43, Dave S wrote:
> On Tuesday 05 December 2006 23:32, Alan Gauld wrote:
> > "Dave S" <[EMAIL PROTECTED]> wrote
> >
> > > Struggling with python & XP again. My app needs to know if a certain
> > > program
> > > is running on my XP box
> > >
> > > os.execl('')
> > > It throws the output to the terminal + I need the exact path to the
> > > executable
> > > (a bit of a trial)
> > >
> > > Any ideas how I can catch the output ?
> >
> > Look at the popen family of functions in the os module, and then
> > look at the subporocess module which supercedees them
> > (but the docs are expressed in tems of the oold functions!)
> >
> > Use subprocess because the older popen functions don't always
> > work reliably on Windows - there is a separate popen as part of
> > the winall package, but I think subprocess.Popen works Ok.
> >
> > There is more on this, including a simple example using subprocess,
> > in my OS topic in my tutorial.
> >
> > HTH,
>
> OK playing around I knocked up some test code ...
>
> #!/usr/bin/env python
> # -*- coding: iso8859_1 -*-
> import subprocess
>
> a = subprocess.Popen('tasklist.exe', bufsize=0, shell=False,
> stdout=subprocess.PIPE, stderr=None, stdin=None, universal_newlines=True)
> op = a.stdout.readlines()
> for i in op:
> #print i
> #print
> pass
>
> #raw_input()
>
> This gives me what I need except when it runs windows flashes up a large
> black terminal window for a split second (yuk)
>
> This test would be performed while my app is running so thats not so good
> :)
>
> Any ideas on how to stop it displaying ?
>
> Dave


10 carrot idiot here :)

change from 
test.py
to 
test.pyw

:)

dave









> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Tim Golden
[Dave S]

| Just looked at WMI - didn't know it existed !
| Am going down the subprocess route first as its 'built in'. 
| If that does not work - hello WMI

Personally, I find having both (and other) tools
in my toolbelt useful -- I've just answered another
question elsewhere about starting and stopping
a process on Win32 using the pywin32 win32process
module, so that's another option...

Generally subprocess is the recommended approach
these days because it's specifically designed to
superseded the various popen/spawn/system choices
previously available. The only problem is that its
general-purpose nature means fossicking around to
mimic the relative simplicity of os.popen:


import os

pipe = os.popen ("tasklist")

# skip unwanted headers
pipe.readline ()
pipe.readline ()
pipe.readline ()

for line in pipe.readlines ():
  print line.split ()[0]



TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Dave S
On Wednesday 06 December 2006 11:04, Kent Johnson 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
>
> I'm not sure if this is helpful, but here is a program that uses
> subprocess.Popen() to capture the output of cmd.exe. It makes a text
> file which contains all the command help. By Scott David Daniels, taken
> from
> http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&;
>
> import subprocess as subp
>
> p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
>   stdout=subp.PIPE, stderr=subp.STDOUT)
> flag = "(@@}"
> print >>p.stdin, "PROMPT", flag
> print >>p.stdin, "HELP"
> print >>p.stdin, "EXIT"
> text = p.stdout.read()
> p.wait()
> helptext = text[text.index(flag + 'HELP') + len(flag) + 4 :
>text.index(flag + 'EXIT')]
> words = [line.split(None, 1)[0]
> for line in helptext.split('\n')
> if line.strip()]
> commands = [word for word in words if word.isupper()]
>
> dest = open('cmd_help.txt', 'wb')
> p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
>   stdout=dest, stderr=subp.STDOUT)
> print >>p.stdin, "PROMPT ()"
> print >>p.stdin, "HELP"
> for command in commands:
>print >>p.stdin, "HELP", command
> print >>p.stdin, "EXIT"
> p.wait()
> dest.close()
>
>
> Kent

The above is usefull, 

I am starting to print out these snippets for reference :). I have got 
subprocess to get the data I am now trying to get rid of the windows terminal 
flashing on my screen.

Cheers

Dave

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Dave S
On Wednesday 06 December 2006 08:48, Tim Golden wrote:
> | Struggling with python & XP again. My app needs to know if a
> | certain program is running on my XP box
>
> As a complete alternative, consider using WMI:
>
> 
> import os, sys
> import wmi
>
> c = wmi.WMI ()
> for process in c.Win32_Process (Name="excel.exe"):
>   print "Excel is running"
>   break
> else:
>   print "Excel is not running"
>
> 
>
> (Uses: http://timgolden.me.uk/python/wmi.html)
>
> TJG

Just looked at WMI - didn't know it existed !
Am going down the subprocess route first as its 'built in'. If that does not 
work - hello WMI

Cheers

Dave








>
> 
> This e-mail has been scanned for all viruses by Star. The
> service is powered by MessageLabs. For more information on a proactive
> anti-virus service working around the clock, around the globe, visit:
> http://www.star.net.uk
> 
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Dave S
On Tuesday 05 December 2006 23:32, Alan Gauld wrote:
> "Dave S" <[EMAIL PROTECTED]> wrote
>
> > Struggling with python & XP again. My app needs to know if a certain
> > program
> > is running on my XP box
> >
> > os.execl('')
> > It throws the output to the terminal + I need the exact path to the
> > executable
> > (a bit of a trial)
> >
> > Any ideas how I can catch the output ?
>
> Look at the popen family of functions in the os module, and then
> look at the subporocess module which supercedees them
> (but the docs are expressed in tems of the oold functions!)
>
> Use subprocess because the older popen functions don't always
> work reliably on Windows - there is a separate popen as part of
> the winall package, but I think subprocess.Popen works Ok.
>
> There is more on this, including a simple example using subprocess,
> in my OS topic in my tutorial.
>
> HTH,

OK playing around I knocked up some test code ...

#!/usr/bin/env python
# -*- coding: iso8859_1 -*- 
import subprocess

a = subprocess.Popen('tasklist.exe', bufsize=0, shell=False, 
stdout=subprocess.PIPE, stderr=None, stdin=None, universal_newlines=True)
op = a.stdout.readlines()
for i in op:
#print i
#print
pass

#raw_input()

This gives me what I need except when it runs windows flashes up a large black 
terminal window for a split second (yuk) 

This test would be performed while my app is running so thats not so good :)

Any ideas on how to stop it displaying ?

Dave
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Kent Johnson
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

I'm not sure if this is helpful, but here is a program that uses 
subprocess.Popen() to capture the output of cmd.exe. It makes a text 
file which contains all the command help. By Scott David Daniels, taken from
http://groups.google.com/group/comp.lang.python/msg/9fa3a3c287e8e2a3?hl=en&;

import subprocess as subp

p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
  stdout=subp.PIPE, stderr=subp.STDOUT)
flag = "(@@}"
print >>p.stdin, "PROMPT", flag
print >>p.stdin, "HELP"
print >>p.stdin, "EXIT"
text = p.stdout.read()
p.wait()
helptext = text[text.index(flag + 'HELP') + len(flag) + 4 :
   text.index(flag + 'EXIT')]
words = [line.split(None, 1)[0]
for line in helptext.split('\n')
if line.strip()]
commands = [word for word in words if word.isupper()]

dest = open('cmd_help.txt', 'wb')
p = subp.Popen("cmd.exe /X/D/F:ON", stdin=subp.PIPE,
  stdout=dest, stderr=subp.STDOUT)
print >>p.stdin, "PROMPT ()"
print >>p.stdin, "HELP"
for command in commands:
   print >>p.stdin, "HELP", command
print >>p.stdin, "EXIT"
p.wait()
dest.close()


Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-06 Thread Tim Golden
| Struggling with python & XP again. My app needs to know if a 
| certain program is running on my XP box

As a complete alternative, consider using WMI:


import os, sys
import wmi

c = wmi.WMI ()
for process in c.Win32_Process (Name="excel.exe"):
  print "Excel is running"
  break
else:
  print "Excel is not running"



(Uses: http://timgolden.me.uk/python/wmi.html)

TJG


This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Dave S
On Tuesday 05 December 2006 23:32, Alan Gauld wrote:
> "Dave S" <[EMAIL PROTECTED]> wrote
>
> > Struggling with python & XP again. My app needs to know if a certain
> > program
> > is running on my XP box
> >
> > os.execl('')
> > It throws the output to the terminal + I need the exact path to the
> > executable
> > (a bit of a trial)
> >
> > Any ideas how I can catch the output ?
>
> Look at the popen family of functions in the os module, and then
> look at the subporocess module which supercedees them
> (but the docs are expressed in tems of the oold functions!)
>
> Use subprocess because the older popen functions don't always
> work reliably on Windows - there is a separate popen as part of
> the winall package, but I think subprocess.Popen works Ok.
>
> There is more on this, including a simple example using subprocess,
> in my OS topic in my tutorial.
>
> HTH,

Thanks for your help - I will take a look. :)

Dave

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Alan Gauld

"Dave S" <[EMAIL PROTECTED]> wrote

> Struggling with python & XP again. My app needs to know if a certain 
> program
> is running on my XP box

> os.execl('')
> It throws the output to the terminal + I need the exact path to the 
> executable
> (a bit of a trial)

> Any ideas how I can catch the output ?

Look at the popen family of functions in the os module, and then
look at the subporocess module which supercedees them
(but the docs are expressed in tems of the oold functions!)

Use subprocess because the older popen functions don't always
work reliably on Windows - there is a separate popen as part of
the winall package, but I think subprocess.Popen works Ok.

There is more on this, including a simple example using subprocess,
in my OS topic in my tutorial.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Dave S
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@python.org
> >>> http://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Luke Paireepinart
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


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

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Dave S
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 :)

Cheers

Dave




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


Re: [Tutor] XP & catching execl o/p ?

2006-12-05 Thread Luke Paireepinart
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.
> Cheers
>
> Dave
>
>  
> ___
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>   

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] XP & catching execl o/p ?

2006-12-05 Thread Dave S
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 ?

Cheers

Dave

 
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor