[Tutor] XP & catching execl o/p ?

Kent Johnson kent37 at tds.net
Wed Dec 6 12:04:37 CET 2006


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



More information about the Tutor mailing list