How do I strip comma from the end of a string? I tried
awk = subprocess.Popen([r"awk", "{print $10}"], stdin=subprocess.PIPE)
awk_stdin = awk.communicate(uptime_stdout)[0]
print awk_stdin
temp = awk_stdin
t = temp.strip(",")
also tried t = temp.rstrip(","), both don’t work.
This is the code:
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()
This is the output:
17:07:32 up 27 days, 37 min, 2 users, load average: 5.23, 5.09, 4.79
5.23,
None
Traceback (most recent call last):
File "uptime.py", line 21, in ?
tem = temp.rstrip("\n")
AttributeError: 'NoneType' object has no attribute 'rstrip'
When you say
then the output of the awk process is printed to stdout (e.g. a terminal).
awk_stdoutis set toNone.awk_stdout.rstrip('\n')raises anAttributeErrorbecauseNonehas no attribute calledrstrip.When you say
then nothing is printed to stdout (e.g. the terminal), and
awk_stdoutgets the output of theawkcommand as a string.