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

作者:Python热爱者 时间:2022-12-07 08:53:36 

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()方法。

来源:https://blog.51cto.com/14246112/2464512

标签:python,调用,脚本
0
投稿

猜你喜欢

  • 实例:ASP与ACCESS链接

    2008-11-21 16:10:00
  • Python3一行代码实现图片文字识别的示例

    2021-11-22 06:45:51
  • 浅谈Python响应式类库RxPy

    2021-12-24 12:44:26
  • 把vgg-face.mat权重迁移到pytorch模型示例

    2021-11-03 16:29:20
  • Ubuntu下使用Python实现游戏制作中的切分图片功能

    2021-02-22 22:55:53
  • Golang中如何对MySQL进行操作详解

    2024-01-28 23:16:53
  • 高效地获取XMLhttp对象

    2010-01-19 13:49:00
  • 详解有关PyCharm安装库失败的问题的解决方法

    2023-05-17 11:06:33
  • Python使用Crypto库实现加密解密的示例详解

    2021-11-10 05:53:10
  • python循环监控远程端口的方法

    2023-08-07 14:53:22
  • MySQL数据库基于sysbench实现OLTP基准测试

    2024-01-28 11:07:14
  • 如何用 Python 处理不平衡数据集

    2023-02-21 07:35:31
  • 三种数据库利用SQL语句进行高效果分页

    2008-11-28 14:52:00
  • 设计模式学习笔记之 - 简单工厂模式

    2009-03-11 13:38:00
  • Python读取Json字典写入Excel表格的方法

    2021-06-20 06:33:58
  • 谈谈我的“分离”观

    2010-08-31 14:47:00
  • python2.7实现FTP文件下载功能

    2021-10-09 19:04:37
  • Centos7下使用yum安装mysql数据库的详细教程(增强版)

    2024-01-13 17:24:52
  • 一文教会你pandas plot各种绘图

    2021-04-29 19:41:11
  • 特效代码:弹出一个淡入淡出的提示框

    2008-05-22 17:11:00
  • asp之家 网络编程 m.aspxhome.com