简单了解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实现的Google IP 可用性检测脚本
Apr 23 Python
将Python代码打包为jar软件的简单方法
Aug 04 Python
Python自动化测试Eclipse+Pydev 搭建开发环境
Aug 15 Python
Pyspider中给爬虫伪造随机请求头的实例
May 07 Python
Flask框架使用DBUtils模块连接数据库操作示例
Jul 20 Python
Django之模型层多表操作的实现
Jan 08 Python
python numpy 按行归一化的实例
Jan 21 Python
python实现nao机器人身体躯干和腿部动作操作
Apr 29 Python
使用python3 实现插入数据到mysql
Mar 02 Python
python opencv 实现读取、显示、写入图像的方法
Jun 08 Python
教你怎么用python selenium实现自动化测试
May 27 Python
Python异常类型以及处理方法汇总
Jun 05 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
自己动手做一个SQL解释器
2006/10/09 PHP
php将textarea数据提交到mysql出现很多空格的解决方法
2014/12/19 PHP
thinkphp5实现微信扫码支付
2019/12/23 PHP
Document对象内容集合(比较全)
2010/09/06 Javascript
js 剪切板的用法(clipboardData.setData)与js match函数介绍
2013/11/19 Javascript
jQuery针对各类元素操作基础教程
2014/08/29 Javascript
简单谈谈jQuery(function(){})与(function(){})(jQuery)
2014/12/19 Javascript
jQuery中用dom操作替代正则表达式
2014/12/29 Javascript
jQuery检测返回值的数据类型
2015/07/13 Javascript
jQuery改变form表单的action,并进行提交的实现代码
2016/05/25 Javascript
jQuery绑定事件的四种方式介绍
2016/10/31 Javascript
AngularJS入门教程之过滤器用法示例
2016/11/02 Javascript
JavaScript实现计数器基础方法
2017/10/10 Javascript
让bootstrap的carousel支持滑动滚屏的实现代码
2017/11/27 Javascript
深入浅析Vue.js 中的 v-for 列表渲染指令
2018/11/19 Javascript
微信小程序实现元素渐入渐出动画效果封装方法
2019/05/18 Javascript
解决Vue中使用keepAlive不缓存问题
2020/08/04 Javascript
JavaScript实现滑块验证解锁
2021/01/07 Javascript
跟老齐学Python之玩转字符串(3)
2014/09/14 Python
python实现根据图标提取分类应用程序实例
2014/09/28 Python
python实现字典(dict)和字符串(string)的相互转换方法
2017/03/01 Python
如何在Cloud Studio上执行Python代码?
2019/08/09 Python
django项目中新增app的2种实现方法
2020/04/01 Python
Web页面中八种创建多列等高(等高列布局)的实现技术
2012/12/24 HTML / CSS
HTML5如何实现元素拖拽
2016/03/11 HTML / CSS
HTML5 3D衣服摇摆动画特效
2016/03/17 HTML / CSS
复古斯堪的纳维亚儿童服装:Baby go Retro
2017/09/09 全球购物
linux比较文件内容的命令是什么
2013/03/04 面试题
报关简历自我评价怎么写
2013/09/19 职场文书
《学棋》教后反思
2014/04/14 职场文书
协议书与合同的区别
2014/04/18 职场文书
2015年上半年物业工作总结
2015/03/30 职场文书
学雷锋主题班会教案
2015/08/13 职场文书
python tkinter模块的简单使用
2021/04/07 Python
win10键盘驱动怎么修复?Win10键盘驱动修复小技巧
2022/04/06 数码科技
Java 垃圾回收超详细讲解记忆集和卡表
2022/04/08 Java/Android