简单了解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删除列表元素del,pop(),remove()及clear()

    python删除列表元素del,pop(),remove()及clear()

    这篇文章主要介绍了python删除列表元素del,pop(),remove()及clear(),列表元素能增加就可以删除,这里要给大家介绍的是删除列表元素,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-03-03
  • PIP和conda 更换国内安装源的方法步骤

    PIP和conda 更换国内安装源的方法步骤

    这篇文章主要介绍了PIP和conda 更换国内安装源的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Python利用pywin32实现自动操作电脑

    Python利用pywin32实现自动操作电脑

    在windows系统上,重复性的操作可以用Python脚本来完成,其中常用的模块是win32gui、win32con、win32api,要使用这三个模块需要先安装pywin32。本文就为大家介绍了如何利用这些模块实现自动操作电脑,感兴趣的可以了解一下
    2022-11-11
  • python中os模块和sys模块的使用详解

    python中os模块和sys模块的使用详解

    本文主要介绍了python中os模块和sys模块的使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • Python代码风格与编程习惯重要吗?

    Python代码风格与编程习惯重要吗?

    实现高内聚,低耦合、结构清晰不臃肿、可读性高、数据冗余性低、高复用、易扩展的代码,并非易事.上到设计模式,下到某个类、方法、函数的构造.在这里我分享一下我自己的代码设计,编写风格,让我们互相学习,需要的朋友可以参考下
    2021-06-06
  • python实现dict版图遍历示例

    python实现dict版图遍历示例

    这篇文章主要介绍了python实现dict版图遍历的示例,需要的朋友可以参考下
    2014-02-02
  • python简单分割文件的方法

    python简单分割文件的方法

    这篇文章主要介绍了python简单分割文件的方法,涉及Python针对文件的读取与写入技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-07-07
  • Python获取Windows桌面路径的三种方法

    Python获取Windows桌面路径的三种方法

    在日常编程工作中,有时我们需要将文件或数据自动保存到用户的桌面上以便于快速访问,在 Windows 操作系统中,可以通过多种方式来获取桌面路径,本文将详细介绍三种常用的方法,需要的朋友可以参考下
    2024-12-12
  • 对python中大文件的导入与导出方法详解

    对python中大文件的导入与导出方法详解

    今天小编就为大家分享一篇对python中大文件的导入与导出方法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • 怎么用Python识别手势数字

    怎么用Python识别手势数字

    今天给大家带来的文章是怎么用Python识别手势数字,文中有非常详细的图文示例,对正在学习python的小伙伴们很有帮助,需要的朋友可以参考下
    2021-06-06

最新评论