3种python调用其他脚本的方法

 更新时间:2020年01月06日 09:56:41   作者:python学习者0  
这篇文章主要介绍了3种python调用其他脚本的方法,每种方法通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

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()

#!/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()

#!/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

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

总结

以上所述是小编给大家介绍的3种python调用其他脚本的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • 跟老齐学Python之啰嗦的除法

    跟老齐学Python之啰嗦的除法

    python 除法运算 比较奇怪,和别的程序语言不大一样。从Python2.2开始,除法运算符除了/之外,又引入了一个除法运算符://,后一种运算符只用于进行整除法。对于除法运算符/,默认时的行为跟Python2.2之前的一样,它视操作数而定,既可以进行整除,也可以进行真除法。
    2014-09-09
  • SQLite5-使用Python来读写数据库

    SQLite5-使用Python来读写数据库

    这篇文章主要介绍了SQLite5-使用Python来读写数据库,数据库的实际应用,通常需要与程序结合起来,通过程序来实现对数据库的访问和读写。本篇先介绍Python语言来调用SQLite数据库,想具体了解的小伙伴可以参考一下</P><P>
    2021-12-12
  • python3实现猜数字游戏

    python3实现猜数字游戏

    这篇文章主要为大家详细介绍了python3实现猜数字游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06
  • Python内建模块struct实例详解

    Python内建模块struct实例详解

    这篇文章主要介绍了Python内建模块struct实例详解,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下
    2018-02-02
  • Python xlwt模块使用代码实例

    Python xlwt模块使用代码实例

    这篇文章主要介绍了Python xlwt模块使用代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 不可错过的十本Python好书

    不可错过的十本Python好书

    不可错过的十本Python好书,分别适合入门、进阶到精深三个不同阶段的人来阅读,感兴趣的小伙伴们可以参考一下
    2017-07-07
  • Tornado高并发处理方法实例代码

    Tornado高并发处理方法实例代码

    这篇文章主要介绍了Tornado高并发处理方法实例代码,具有一定借鉴价值,需要的朋友可以参考下
    2018-01-01
  • Pyinstaller将py打包成exe的实例

    Pyinstaller将py打包成exe的实例

    下面小编就为大家分享一篇Pyinstaller将py打包成exe的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • Django --Xadmin 判断登录者身份实例

    Django --Xadmin 判断登录者身份实例

    这篇文章主要介绍了Django --Xadmin 判断登录者身份实例,具有很好的参考价值,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • influx+grafana自定义python采集数据和一些坑的总结

    influx+grafana自定义python采集数据和一些坑的总结

    一些数据的类型不正确会导致no datapoint的错误,真是令人抓狂,本文就是总结一下采集数据种的一些坑,希望大家可以从中获益
    2018-09-09

最新评论