======== 获得执行命令失败输出的报错 subprocess模块 =======
#获取错误输出mytask = subprocess.Popen('svn update --username %s --password %s' % (svnname, svnpw),shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)#读出错误信息并赋值给stdstr变量stdstr= mytask.stdout.read() #判断有没有输出错误信息if 'svn: E' in stdstr: print 'yes'else: print 'no'
========== 获得执行命令得到当前目录下的文件 os模块 =========
r=os.popen('ls')info=r.readlines()for i in info: i=i.strip('\n') print i
========== 赋值执行命令的结果到变量的方法 os模块 ============
num=os.popen(‘mysql -uroot -pxxx test -e "select *from stu;" | wc -l’)output = num.read()print n