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


Posted in Python onMarch 26, 2020

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 相关文章推荐
python利用hook技术破解https的实例代码
Mar 25 Python
python实现的一个p2p文件传输实例
Jun 04 Python
详解Python中内置的NotImplemented类型的用法
Mar 31 Python
python开发之函数定义实例分析
Nov 12 Python
Ubuntu安装Jupyter Notebook教程
Oct 18 Python
TensorFlow模型保存/载入的两种方法
Mar 08 Python
Python实现的查询mysql数据库并通过邮件发送信息功能
May 17 Python
详解爬虫被封的问题
Apr 23 Python
window7下的python2.7版本和python3.5版本的opencv-python安装过程
Oct 24 Python
Python 剪绳子的多种思路实现(动态规划和贪心)
Feb 24 Python
Python中zipfile压缩文件模块的基本使用教程
Jun 14 Python
Python tornado上传文件的功能
Mar 26 #Python
Python Tornado批量上传图片并显示功能
Mar 26 #Python
python列表删除和多重循环退出原理详解
Mar 26 #Python
执行Python程序时模块报错问题
Mar 26 #Python
python3 正则表达式基础廖雪峰
Mar 25 #Python
python 6.7 编写printTable()函数表格打印(完整代码)
Mar 25 #Python
python实现人机五子棋
Mar 25 #Python
You might like
PHP 查找字符串常用函数介绍
2012/06/07 PHP
去除php注释和去除空格函数分享
2014/03/13 PHP
8个必备的PHP功能开发
2015/10/02 PHP
PHP使用第三方即时获取物流动态实例详解
2017/04/27 PHP
实例讲解YII2中多表关联的使用方法
2017/07/21 PHP
PHP数组基本用法与知识点总结
2020/06/02 PHP
论坛里点击别人帖子下面的回复,回复标题变成“回复 24# 的帖子”
2009/06/14 Javascript
js点击更换背景颜色或图片的实例代码
2013/06/25 Javascript
jquery动态添加删除一行数据示例
2014/06/12 Javascript
JavaScript字符串常用类使用方法汇总
2015/04/14 Javascript
在Node.js应用中读写Redis数据库的简单方法
2015/06/30 Javascript
详解JavaScript对象和数组
2015/12/03 Javascript
简介EasyUI datagrid editor combogrid搜索框的实现
2016/04/01 Javascript
Ubuntu 16.04 64位中搭建Node.js开发环境教程
2016/10/19 Javascript
基于JavaScript实现图片剪切效果
2017/03/07 Javascript
通过V8源码看一个关于JS数组排序的诡异问题
2017/08/14 Javascript
Angular 作用域scope的具体使用
2017/12/11 Javascript
jquery点击回车键实现登录效果并默认焦点的方法
2018/03/09 jQuery
python3实现暴力穷举博客园密码
2016/06/19 Python
Python操作MongoDB数据库的方法示例
2018/01/04 Python
Python查找第n个子串的技巧分享
2018/06/27 Python
在windows下使用python进行串口通讯的方法
2019/07/02 Python
postman传递当前时间戳实例详解
2019/09/14 Python
python tkinter图形界面代码统计工具
2019/09/18 Python
Python集合基本概念与相关操作实例分析
2019/10/30 Python
Pycharm中import torch报错的快速解决方法
2020/03/05 Python
python中return如何写
2020/06/18 Python
canvas绘制视频封面的方法
2018/02/05 HTML / CSS
竞选学习委员演讲稿
2014/09/01 职场文书
解除劳动关系协议书范文
2014/09/11 职场文书
2014党支部对照检查材料思想汇报
2014/10/05 职场文书
查摆问题自查报告范文
2014/10/13 职场文书
业务员岗位职责
2015/02/03 职场文书
导游词之安徽醉翁亭
2020/01/10 职场文书
Nginx 502 Bad Gateway错误原因及解决方案
2021/03/31 Servers
win10输入法不见了只能打出字母怎么解决?
2022/08/05 数码科技