我的代码有一个ifelse条件,但我认为代码没有检查if条件,而是直接转到else条件

2024-10-01 19:17:29 发布

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

我有这个代码来改变文件系统的大小。问题是,即使if条件满足,它也不会进入if条件,我根本没有if条件来检查if条件。它直接进入else状态。你知道吗

运行代码后的结果

post-install-ray.py chpasswd

chpasswd root:Deepsky24

df -g | grep -w / | awk '{ print $3 }' 0.89

df -g | grep -w /usr | awk '{print $3 }' 5.81

chfs -a size=+4G / Filesystem size changed to 10354688

代码:

import subprocess
import sys
import pexpect
from pexpect import pxssh
import logging

password = "Deepsky24"
hostname = "host"
username = "root"

def change_filesystem():
    child = pxssh.pxssh()
    child.login(hostname, username, password)
    child.sendline("df -g | grep -w / | awk '{ print $3 }'")
    child.prompt()
    output1 = child.before
    print output1
    child.sendline("df -g | grep -w /usr | awk '{print $3 }'")
    child.prompt()
    output2 = child.before
    print output2
    if (output1 <= 1 and output2 >= 4):

        child.sendline('chfs -a size=-3G /usr')
        child.prompt()
        print(child.before)
        child.sendline('chfs -a size=+3G /')
        child.prompt()
        print(child.before)

    else:

        child.sendline('chfs -a size=+4G /')
        child.prompt()
        print(child.before)

if __name__== "__main__":

        change_filesystem()

Tags: 代码importchilddfsizeifusr条件
2条回答

你的两个输出仍然是字符串,你需要把它们转换成浮点数

output1 = float(child.before)
output2 = float(child.before)

我猜你的电脑在Python2上。你知道吗

child.before返回字符串。 将字符串与整数进行比较会得到意外的结果。 用int()float()转换输出

对于python3,你会有一个例外

相关问题 更多 >

    热门问题