专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

怎么样在shell脚本中调用python脚本_python

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/3/9 3:51:44       共计:3586 浏览

怎么样在shell脚本中调用python脚本?

1、os.system(cmd)

缺点:不能获取返回值

2、os.popen(cmd)

要得到命令的输出内容,只需再调用下read()或readlines()等

例:a=os.popen(cmd).read()

3、commands模块,其实也是对popen的封装。

此模块主要有如下方法:

commands.getstatusoutput(cmd)返回(status, output).

commands.getoutput(cmd)只返回输出结果

commands.getstatus(file)返回ls -ld file的执行结果字符串,调用了getoutput

例:

>>> import commands

>>> commands.getstatusoutput('ls /bin/ls')

(0, '/bin/ls')

>>> commands.getstatusoutput('cat /bin/junk')

(256, 'cat: /bin/junk: No such file or directory')

>>> commands.getstatusoutput('/bin/junk')

(256, 'sh: /bin/junk: not found')

>>> commands.getoutput('ls /bin/ls')

'/bin/ls'

>>> commands.getstatus('/bin/ls')

'-rwxr-xr-x1 root13352 Oct 141994 /bin/ls'

来源:麦子学院

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:做自媒体真能赚到几十万吗_PHP基础 | ·下一条:如何使用ZArchiver对压缩文件进行处理_java

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有