Err, how about the venerable:
if len(str) > 0:
if str[-1:] == ",":
str = str[:-1]
On second thought, rstrip
itself should work fine, so there's something about the string you're getting from awk
that's not quite what you expect. We'll need to see that.
I suspect it's because your string doesn't actually end with a comma. When you run:
str = "hello,"
print str.rstrip(",")
str = "hello,\n"
print str.rstrip(",")
print str.rstrip(",\n")
the output is:
hello
hello,
hello
In other words, if there's a newline at the end of the string as well as a comma, you'll need to rstrip
both characters with ",\n"
.
Okay, based on your comment, here's what you're trying:
uptime = subprocess.Popen([r"uptime"], stdout=subprocess.PIPE)
uptime_stdout = uptime.communicate()[0]
print uptime_stdout
awk = subprocess.Popen([r"awk", "{print $11}"], stdin=subprocess.PIPE)
awk_stdin = awk.communicate(uptime_stdout)[0]
print repr(awk_stdin)
temp = awk_stdin
tem = temp.rstrip("\n")
logfile = open('/usr/src/python/uptime.log', 'a')
logfile.write(tem + "\n")
logfile.close()
What are you actually getting from your two print
statements and what is being appended to the log file?
My particular uptime
doesn't have a $11
:
23:43:10 up 5:10, 0 users, load average: 0.00, 0.00, 0.00
but yours may be different.
Still, we need to see the output of your script.