python 获取命令的返回值以及命令执行的状态


[root@xuhui-1 foundation]# cat command.py 
#!/bin/env python
#_*_conding:UTF-8_*_
import commands
status,output=commands.getstatusoutput('ls')
print 'status:'+str(status)+''
print 'stdinoutput:\n'+output+''
[root@xuhui-1 foundation]# ./command.py 
status:0
stdinoutput:
array.py
char-list.py
command.py
diff.py
readurl.py
scanraid.py
test2.py

 python raw_input 函数的返回值均为str类型


python 高亮文字方法:

>>> name = 'xuhui'
>>> print 'name:\033[31;1m%s \033[0m' %(name)
name:xuhui


Python 循环中的break与continue的区别

continue是跳过一个循环

break是直接跳出循环

example:

[root@xuhui-1 foundation]# cat sentence-for2.py 

[root@xuhui-1 foundation]# cat sentence-for2.py 
#!/bin/env python
for i in range(10):
	if i == 5:
		print "i=%s" %(i)
		continue
		#原本应该显示5的时候跳过循环
	elif i >= 7:
		break
		#原本应该显示7的时候跳出循环
	print i
[root@xuhui-1 foundation]# ./sentence-for2.py 
0
1
2
3
4
i=5
6