您的位置 首页 > 腾讯云社区

Python 入门---音视频_李超

Python 调用 Shell 脚本准备 shell 脚本 hello.sh#! /usr/bin/ssh echo "hello world!" echo "succeed"使用os.system方法#! /usr/local/bin/python import os v_return_status=os.system( 'sh hello.sh') print "v_return_status=" +str(v_return_status)

输出结果:

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 循环里的执行语句前面一定要缩进。 ---来自腾讯云社区的---音视频_李超

关于作者: 瞎采新闻

这里可以显示个人介绍!这里可以显示个人介绍!

热门文章

留言与评论(共有 0 条评论)
   
验证码: