简单了解python调用其他脚本方法实例

 更新时间:2020年03月26日 11:06:06   作者:Python热爱者  
这篇文章主要介绍了简单了解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()方法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Python之ascii转中文的实现

    Python之ascii转中文的实现

    这篇文章主要介绍了Python之ascii转中文的实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-05-05
  • python 根据pid杀死相应进程的方法

    python 根据pid杀死相应进程的方法

    下面小编就为大家带来一篇python 根据pid杀死相应进程的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • Django单元测试的具体使用

    Django单元测试的具体使用

    Django提供了一套强大的测试工具来帮助开发者编写和运行单元测试,本文就来介绍一下Django中的单元测试,包括测试原理、编写测试用例和运行测试,感兴趣的可以了解一下
    2023-11-11
  • 教女朋友学Python(一)运行环境搭建

    教女朋友学Python(一)运行环境搭建

    这篇文章主要介绍了教女朋友学Python(一)运行环境搭建,具有一定借鉴价值,需要的朋友可以参考下。
    2017-11-11
  • python随机生成大小写字母数字混合密码(仅20行代码)

    python随机生成大小写字母数字混合密码(仅20行代码)

    这篇文章主要介绍了python随机生成大小写字母数字混合密码,主要是利用random模块随机生成数字,大小写字母,通过循环次数来实现此功能,需要的朋友可以参考下
    2020-02-02
  • Python导入torch包的完整方法过程

    Python导入torch包的完整方法过程

    这篇文章主要给大家介绍了关于Python导入torch包的完整方法, python torch又称PyTorach,是一个以Python优先的深度学习框架,一个开源的Python机器学习库,用于自然语言处理等应用程序,需要的朋友可以参考下
    2023-12-12
  • python疲劳驾驶困倦低头检测功能的实现

    python疲劳驾驶困倦低头检测功能的实现

    这篇文章主要介绍了python疲劳驾驶困倦低头检测,该系统可以检测一个人在开车时是否困倦,及时提醒,做到安全隐患排查,对实现代码感兴趣的朋友一起看看吧
    2022-04-04
  • Python itertools.product方法代码实例

    Python itertools.product方法代码实例

    这篇文章主要介绍了Python itertools.product方法代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03
  • 详解pandas.DataFrame.plot() 画图函数

    详解pandas.DataFrame.plot() 画图函数

    这篇文章主要介绍了详解pandas.DataFrame.plot()画图函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-06-06
  • windowns使用PySpark环境配置和基本操作

    windowns使用PySpark环境配置和基本操作

    pyspark是Spark对Python的api接口,可以在Python环境中通过调用pyspark模块来操作spark,这篇文章主要介绍了windowns使用PySpark环境配置和基本操作,感兴趣的可以了解一下
    2021-05-05

最新评论