main() 中的逻辑似乎没有达到 else 条件 - 为什么
The logic in main() does not seem to hit the else condition - why
我在下面有一个函数,它查看路径,并确定使用了多少磁盘 space。
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'
我在你的代码中做了一些改动。
- 您没有从函数返回任何值
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
从 >
更改为 <
以检查其他条件并且有效。
示例 运行 2 :
===== RESTART: C:/filesOperation.py =====
Space : 255
plennnty!
我在下面有一个函数,它查看路径,并确定使用了多少磁盘 space。
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'
我在你的代码中做了一些改动。
- 您没有从函数返回任何值
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
从 >
更改为 <
以检查其他条件并且有效。
示例 运行 2 :
===== RESTART: C:/filesOperation.py =====
Space : 255
plennnty!