2017-09-16 28 views
-1

我有一个函数在下面,它查看一个路径,并确定使用多少磁盘空间。main()中的逻辑似乎没有达到else条件 - 为什么

def check_disk_space(): 
    import os 
    cmdparts = ["echo $(df --output=pcent ", ") | tr -d 'Use% '"] 
       check_used_disk_space_cmd = cmdparts[0] + "a/path" + cmdparts[1] 
    os.system(check_used_disk_space_cmd) 

def main(): 
    used_disk_space = check_disk_space() 
    print type(used_disk_space) 
    if int(used_disk_space) > 80: 
     print "need more" 
    else: 
     print "plennnty!" 
     print type(used_disk_space) 
main() 

check_disk_space()返回85

更新:它出现check_disk_space()创建一个NoneType对象?我得到这个错误: TypeError: int() argument must be a string or a number, not 'NoneType'

+2

因为你没有实际调用的函数,即使你没有它不反正返回任何东西。 –

+0

您需要捕获os.system调用的输出并将其转换为check_disk_space方法中的INT或FLOAT。我会看看[subprocess.Popen](https://docs.python.org/2/library/subprocess.html#popen-constructor)类。 – Kyle

+0

请参阅更新的主要 - 当它真的应该'需要更多' – John

回答

1

我在你的代码中改变了一些东西。

  1. 你是不是从你的函数
  2. import os返回任何值,可以移动到文件的顶部

注意:我已经添加了括号来打印声明。

import os 

def check_disk_space(): 
    """ 
    check_disk_space() checks the available space of a specified path 
    """ 
    cmdparts = ["echo $(df --output=pcent ", ") | tr -d 'Use% '"] 
    check_used_disk_space_cmd = cmdparts[0] + "C:/Users/jgosalia/Desktop" + cmdparts[1] 
    return os.system(check_used_disk_space_cmd) 

def main(): 
    space = check_disk_space() 
    print("Space : " + str(space)) 
    if space > 95: 
     print ("need more") 
    else: 
     print ("plennnty!") 

main() 

样品试验1:

===== RESTART: C:/filesOperation.py ===== 
Space : 255 
need more 

改变if condition><检查else条件和它的作品。

样品试验2:

===== RESTART: C:/filesOperation.py ===== 
Space : 255 
plennnty! 
相关问题