getting a process's PID

Sebastian 'lunar' Wiesner basti.wiesner at gmx.net
Wed Dec 27 15:20:06 EST 2006


eldorado <eldorado at io.com> typed

[snip]
> This looks cleaner than the way I was going.  I created a file
> called ps.py
> 
> #!/usr/local/bin/python
> import os
> g = os.popen("ps -e -o pid,command")
> for line in g.readlines():
>          if 'HUB' in line:
>                  pid = line.strip().split(' ')[0]
>                  break
> print pid
> 
> When I run ps.py I get the following error.
> 
> Traceback (innermost last):
>    File "./ps.py", line 5, in ?
>      if 'HUB' in line:
> TypeError: string member test needs char left operand

Strange!? On my system with Python 2.4 I don't get this error. It is
likely to be a problem of your really ancient python version. Do I
guess correctly from your previous postings, that you're still using
version 1.4?. 

The only thing, you could advice you to, is to replace the line with the
following code, which should do very much the same thing:

>>>> if line.find('HUB') > -1:

If you are really using version 1.4, you should replace the next line,
too, because string methods came after 1.4:

>>>> pid = string.split(string.strip(line), ' ')[0]

Bye
Sebastian Wiesner

-- 
Freedom is always the freedom of dissenters.
                                      (Rosa Luxemburg)



More information about the Python-list mailing list