输出结果:
hello world! succeed v_return_status=0使用os.popen方法无返回终端,只打印输出内容
获取shell print 语句内容一次性打印p=os.popen('sh hello.sh') x=p.read() print x p.close()输出结果: hello world! succeed
获取shell print 语句内容,按照行读取打印p=os.popen('sh hello.sh') x=p.readlines() for line in x: print 'ssss='+line输出结果: ssss=hello world! ssss=succeed
使用commands.getstatusoutput() 方法该方法可以获得到返回值和输出,非常好用。
-使用 commands.getstatusoutput() 方法获得到返回值和输出
(status, output) = commands.getstatusoutput('sh hello.sh') print status, output输出结果: 0 hello world! succeed
Python for 循环for line in x: print line特别需要注意的地方:
for 语句的后面一定要有 ':' 。for 循环里的执行语句前面一定要缩进。 ---来自腾讯云社区的---音视频_李超
微信扫一扫打赏
支付宝扫一扫打赏