Jquery中文网 www.jquerycn.cn
Jquery中文网 >  Python编程  >  Python入门  >  正文 如何调用python中的shell脚本?

如何调用python中的shell脚本?

发布时间:2020-12-09   编辑:www.jquerycn.cn
jquery中文网为您提供如何调用python中的shell脚本?等资源,欢迎您收藏本站,我们将为您提供最新的如何调用python中的shell脚本?资源

相信大家对于这块了解应该挺丰富了吧,小编早几期一直给大家教大家相关的内容,不知道现在在给大家提及,脑子里会不会有些许印象呢?或者,大家可不可以给小编完成这次我们文章的问题呢?最起码大家心里应该知道怎么去调用一些脚本了吧,那大家根据自己的想像,在看下下文小编给的内容,结合起来,优化下吧~

1. python调用shell方法os.system()

#!/usr/local/bin/python3.7
import time
import os
 
count = 0
n = os.system('sh b.sh')
while True:
    count = count   1
    if count == 8:
      print('this count is:',count)
      break
    else:
      time.sleep(1)
      print('this count is:',count)   
 
print('Good Bye')

shell脚本如下:

#!/bin/sh
echo "hello world"

运行结果:

[python@master2 while]$ python a.py
hello world
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye

2. python调用shell方法os.popen()

#!/usr/local/bin/python3.7
import time
import os
 
count = 0
n = os.system('sh b.sh')
while True:
    count = count   1
    if count == 8:
      print('this count is:',count)
      break
    else:
      time.sleep(1)
      print('this count is:',count)   
 
print('Good Bye')

运行结果:

[python@master2 while]$ python a.py
<os._wrap_close object at 0x7f7f89377940>
['hello world\n']
this count is: 1
this count is: 2
this count is: 3
this count is: 4
this count is: 5
this count is: 6
this count is: 7
this count is: 8
Good Bye

好啦,大家看下是否跟平常调用方式一不一样呢?大概都能看出来关于python调用脚本时候,大致都是这样子的吧,那大家如果遇到类似问题,可以举一反三学习哦~

您可能感兴趣的文章:
python shell是什么
linux如何运行python脚本
jmeter怎么调用python
python中怎么运行shell脚本
shell脚本向mysql表批量插入数据
inux shell初级入门教程
如何用python写脚本
bash shell脚本执行的几种方法
shell命令行参数用法简介
python脚本如何测试手机

[关闭]