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调用其他脚本的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

相关文章

  • pytorch中常用的乘法运算及相关的运算符(@和*)

    pytorch中常用的乘法运算及相关的运算符(@和*)

    pytorch是深度学习框架,而深度学习其实本质就是一大堆矩阵乘法,最后用来模拟一个高维拟合函数,下面这篇文章主要给大家介绍了关于pytorch中常用的乘法运算及相关的运算符(@和*)的相关资料,需要的朋友可以参考下
    2022-01-01
  • python实现Oracle查询分组的方法示例

    python实现Oracle查询分组的方法示例

    这篇文章主要介绍了python实现Oracle查询分组的方法,结合实例形式分析了python使用group by子句及having子句实现Oracle查询分组的相关操作技巧,需要的朋友可以参考下
    2020-04-04
  • pandas实现对一列/多列进行数据区间筛选

    pandas实现对一列/多列进行数据区间筛选

    这篇文章主要介绍了pandas实现对一列/多列进行数据区间筛选方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • Python接口自动化之浅析requests模块post请求

    Python接口自动化之浅析requests模块post请求

    这篇文章Python接口自动化之浅析requests模块post请求,以下主要介绍requests模块中的post请求的使用,post源码,data、json参数应用场景及实战
    2021-08-08
  • Python的净值数据接口调用示例分享

    Python的净值数据接口调用示例分享

    这篇文章主要介绍了Python的净值数据接口调用示例分享的相关资料,需要的朋友可以参考下
    2016-03-03
  • python图的深度优先和广度优先算法实例分析

    python图的深度优先和广度优先算法实例分析

    这篇文章主要介绍了python图的深度优先和广度优先算法,结合实例形式分析了图的深度优先算法与广度优先算法相关概念、原理、实现技巧与操作注意事项,需要的朋友可以参考下
    2019-10-10
  • Python基于BeautifulSoup爬取京东商品信息

    Python基于BeautifulSoup爬取京东商品信息

    这篇文章主要介绍了Python基于BeautifulSoup爬取京东商品信息,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • Python新手如何理解循环加载模块

    Python新手如何理解循环加载模块

    在本篇文章里小编给大家整理了关于Python新手如何理解循环加载模块相关知识点,有需要的朋友们可以学习下。
    2020-05-05
  • matplotlib subplots 调整子图间矩的实例

    matplotlib subplots 调整子图间矩的实例

    今天小编就为大家分享一篇matplotlib subplots 调整子图间矩的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python mysql断开重连的实现方法

    python mysql断开重连的实现方法

    这篇文章主要介绍了python mysql断开重连的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07

最新评论