Re: getting a process's PID



On Wed, 27 Dec 2006, Erik Johnson wrote:

"eldorado" <eldorado@xxxxxx> wrote in message
news:20061227102939.L20663@xxxxxxxxxxxxxx
Hello,

I am trying to get python to give me the PID of a process (in this case
HUB). I have it working, except for the fact that the output includes
\012 (newline). Is there a way to ask python not to give me a newline?

Python 1.4 (Oct 14 1997) [C]
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
import os
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']


There's more than one way to do it! (Oh, sorry, that's Perl...)

The two most standard ways would be to call strip() on your string to get
one sans both leading and trialing whitespace

print h.strip()

or if you know exactly what you've got (i.e., the newline you don't want is
just the last character), you can just get rid of it:

h = h[:-1]


Thanks for the help, however it doesnt look like those two solutions quite work:



g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
h
['87334\012']
h = h[:-1]
h
[]



import string
g = os.popen("ps -ef | grep HUB | grep -v grep | awk '{ print $2 }'")
h = g.readlines()
g.close()
print h.strip()
File "<stdin>", line 1
print h.strip()
^
SyntaxError: invalid syntax

I looked up the syntax for print and it looks correct (at least to me ;)


--
Randomly generated signature
You can go anywhere you want if you look serious and carry a clipboard.
.



Relevant Pages

  • Re: getting a processs PID
    ... I am trying to get python to give me the PID of a process (in this case ... HUB). ... Is there a way to ask python not to give me a newline? ...
    (comp.lang.python)
  • getting a processs PID
    ... I am trying to get python to give me the PID of a process (in this case HUB). ... except for the fact that the output includes \012 (newline). ...
    (comp.lang.python)
  • Re: Unification of Methods and Functions
    ... >would have the greatest concentration of unbound method use (my reasoning was ... staticmethod syntax is relatively new, and there is a long tradition ... in Python of using various alternatives to static methods. ...
    (comp.lang.python)
  • Re: Unification of Methods and Functions
    ... Moving them outside the ... inside and add the 'staticmethod' wrapper. ... and clients, who don't have time to get proficient in Python, and only ... That is one benefit of the proposed syntax. ...
    (comp.lang.python)
  • Re: - E04 - Leadership! Google, Guido van Rossum, PSF
    ... * BS in Computer Science or equivalent experience. ... you seem to know python. ... is a syntax trifle. ... sure to note the reciprocal "LIMITATION: ...
    (comp.lang.python)