3种python调用其他脚本的方法,你还知道其他的方法吗?
admin
2023-01-20 12:01:07
0

1.用python调用python脚本

#!/usr/local/bin/python3.7
import time
import os 

count = 0
str = ('python b.py')
result1 = os.system(str)
print(result1)
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脚本b.py如下:

#!/usr/local/bin/python3.7
print('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.system()

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/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

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

'''
遇到问题没人解答?小编创建了一个Python学习交流QQ群:579817333 
寻找有志同道合的小伙伴,互帮互助,群里还有不错的视频学习教程和PDF电子书!
'''
#!/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 

['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

os.system.popen() 这个方法会打开一个管道,返回结果是一个连接管道的文件对象,该文件对象的操作方法同open(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会将错误信息输出到stdout,并返回一个空字符串。这里官方也表示subprocess模块已经实现了更为强大的subprocess.Popen()方法。

相关内容

热门资讯

【第一财经】“熊猫来了.怎么开... 有 亲,根据资深记者爆料熊猫来了是可以开挂的,确实有挂(咨询软件无需打开...
今日重大通报“圣盛晃晃麻将.真... 网上科普关于“圣盛晃晃麻将有没有挂”话题很是火热,小编也是针对圣盛晃晃麻将作*弊开挂的方法以及开挂对...
最新引进“天天十三水.到底有挂... 最新引进“天天十三水.到底有挂吗?”必胜开挂神器您好,天天十三水这个游戏其实有挂的,确实是有挂的,需...
终于了解“湘乐.究竟有挂吗?”... 您好:湘乐这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款游戏中打...
【今日要闻】“旺旺福建麻将.究... 您好:旺旺福建麻将这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9784099】很多玩家在这款...
印度航空找到“遗忘”13年的波... 【环球网报道 记者 赵建东】据新加坡《海峡时报》等多家外媒18日报道,印度航空近日承认,该公司一架波...
我来教教您“浙衢麻将.开挂神器... 我来教教您“浙衢麻将.开挂神器?”原来真的有挂您好,浙衢麻将这个游戏其实有挂的,确实是有挂的,需要了...
视频|这场大赛 聚青创之力绘“... 当“双碳”目标绘就发展蓝图,当绿色创新点燃时代引擎,聚青春之力赋能绿色低碳产业高质量发展正当时。 江...
构建多元治疗场景技术平台 企业的发展轨迹,总是深植于特定时代的需要与使命的召唤之中。微电生理创立于2010年,自诞生之初便将目...
今日重大发现“白金岛.开挂器?... 网上科普关于“白金岛有没有挂”话题很是火热,小编也是针对白金岛作*弊开挂的方法以及开挂对应的知识点,...