for 循环的问题
#!/usr/bin/env pythonimport commands
df_stat=commands.getoutput('df -hk')
df_stat1=df_stat.split('\n')
#df_check=commands.getoutput('/home/nmsadm/hc/bin/dir_check.sh')
for i in range(len(df_stat1)):
if len(df_stat1)==1:
del df_stat1
else:
df_stat11=df_stat1.split()[-2]
if int(df_stat11)>70:
print 'Waring:following disk is overload:\n'
print '%s\n' %df_stat1
else:
print 'all disk usage is ok!'
这个脚本执行结果
如果值大于70:那么循环执行2个print 语句,例如:
Waring:following disk is overload:
/home/a 100G 77G 22G 78% /home/a
Waring:following disk is overload:
/home/b 100G 77G 22G 78% /home/b
。。。。。。。。。。。。。。。
请问如何才能达到效果(将第一个print语句移动到for循环开始前就无法与else语句做判断):
Waring:following disk is overload:
/home/a 100G 77G 22G 78% /home/a
/home/b 100G 77G 22G 78% /home/b
另如果值都是小于70的:那么重复打印很多遍“all disk usage is ok!”,如何才能打印一遍呢。。。谢谢!
回复 1# seacnboy
可以把代码简单调节下:
#!/usr/bin/env python
import commands
df_stat=commands.getoutput('df -hk').split('\n')
all_ok = True
for line in df_stat:
percentage = line.split()[-2]
if int(percentage[:-1])>70:
print 'Waring:following disk is overload:\n %s\n' % line
all_ok = False
else: // 前面没有缩进,是在For执行后判断
if all_ok == True:
print 'all disk usage is ok!'
谢谢!回复 2# icymirror
页:
[1]