[英]Python script to check Namenode status
I am new to Python and currently only learned a few things by researching online. 我是Python的新手,目前仅通过在线研究学到了一些东西。 Just wanted to know why am i getting "error" as the output rather than "success".
只想知道为什么我得到“错误”作为输出而不是“成功”。 Please see code below:
请参见下面的代码:
#! /usr/bin/python -v
import os
import subprocess
f = os.popen("hdfs haadmin -getServiceState nn2")
now = f.read()
status = "active"
if now == status:
print "success"
else:
print 'error'
Thanks, Anil 谢谢阿尼尔
(moving comment to an answer) (将评论移至答案)
Whenever pulling text from something like popen or anywhere, I like to use .strip()
and usually .lower()
to clean off the newlines and extra spaces. 每当从popen之类的地方提取文本时,我都喜欢使用
.strip()
和通常.lower()
来清除换行符和多余的空格。
#! /usr/bin/python -v
import os
import subprocess
f = os.popen("hdfs haadmin -getServiceState nn2")
now = f.read().lower().strip() # add strip here
if now == "active":
print "success"
else:
print 'error:', now # why not print out what it output?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.