当前位置: 首页 > 图灵资讯 > 行业资讯> 如何调用python中的shell脚本?

如何调用python中的shell脚本?

发布时间:2024-11-24 16:09:26

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

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

#!/usr/local/bin/python3.7
importtime
importos

count=0
n=os.system('shb.sh')
whileTrue:
count=count+1
ifcount==8:
print('thiscountis:',count)
break
else:
time.sleep(1)
print('thiscountis:',count)

print('GoodBye')

shell脚本如下:

#!/bin/sh
echo"helloworld"

运行结果:

[python@master2while]$pythona.py
helloworld
thiscountis:1
thiscountis:2
thiscountis:3
thiscountis:4
thiscountis:5
thiscountis:6
thiscountis:7
thiscountis:8
GoodBye

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

#!/usr/local/bin/python3.7
importtime
importos

count=0
n=os.system('shb.sh')
whileTrue:
count=count+1
ifcount==8:
print('thiscountis:',count)
break
else:
time.sleep(1)
print('thiscountis:',count)

print('GoodBye')

运行结果:

[python@master2while]$pythona.py
<os._wrap_closeobjectat0x7f7f89377940>
['helloworld\n']
thiscountis:1
thiscountis:2
thiscountis:3
thiscountis:4
thiscountis:5
thiscountis:6
thiscountis:7
thiscountis:8
GoodBye

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

相关文章

如何让vim支持python3

如何让vim支持python3

2025-09-12
python2.7和3.6区别有哪些

python2.7和3.6区别有哪些

2025-09-12
python3有serial库吗

python3有serial库吗

2025-09-12
python中w、r表示什么意思

python中w、r表示什么意思

2025-09-12
python中如何把list变成字符串

python中如何把list变成字符串

2025-09-12
python命名空间是什么

python命名空间是什么

2025-09-12