gpt4 book ai didi

用于检查 Namenode 状态的 Python 脚本

转载 作者:可可西里 更新时间:2023-11-01 16:11:29 26 4
gpt4 key购买 nike

我是 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'

谢谢,阿尼尔

最佳答案

(将评论移至答案)

每当从诸如 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?

关于用于检查 Namenode 状态的 Python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29975811/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com