呃,那尊者呢:if len(str) > 0:
if str[-1:] == ",":
str = str[:-1]
再想一想,rstrip本身应该工作得很好,所以从awk得到的字符串有点不符合您的期望。我们得看看。
我怀疑是因为你的字符串没有以逗号结尾。当你跑步时:str = "hello,"
print str.rstrip(",")
str = "hello,\n"
print str.rstrip(",")
print str.rstrip(",\n")
输出为:hello
hello,
hello
换言之,如果字符串末尾有一个换行符和一个逗号,则需要同时使用rstrip和",\n"两个字符。
好吧,根据你的评论,以下是你正在尝试的: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()
实际上,从两个print语句中获取的是什么?日志文件中附加了什么?
我特别的uptime没有
但你的可能不同。
不过,我们还需要查看脚本的输出。