检查Namenode状态的Python脚本

2024-09-29 01:18:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉,目前只通过在线研究学到了一些东西。只是想知道为什么我得到的是“错误”而不是“成功”。请参见以下代码:

#! /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'

谢谢, 阿尼尔


Tags: 代码importreadbinosusrstatus错误
1条回答
网友
1楼 · 发布于 2024-09-29 01:18:48

(将评论移到答案)

无论何时从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?

相关问题 更多 >