简单了解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的源码浅要剖析Python的内存管理
Apr 16 Python
Python函数中*args和**kwargs来传递变长参数的用法
Jan 26 Python
python3调用R的示例代码
Feb 23 Python
Python编程中NotImplementedError的使用方法
Apr 21 Python
Python3使用正则表达式爬取内涵段子示例
Apr 22 Python
Python基于最小二乘法实现曲线拟合示例
Jun 14 Python
Python中按值来获取指定的键
Mar 04 Python
Python3 itchat实现微信定时发送群消息的实例代码
Jul 12 Python
使用python分析统计自己微信朋友的信息
Jul 19 Python
Python 中使用 PyMySQL模块操作数据库的方法
Nov 10 Python
Nginx+Uwsgi+Django 项目部署到服务器的思路详解
May 08 Python
Python如何配置环境变量详解
May 18 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
在MongoDB中模拟Auto Increment的php代码
2011/03/06 PHP
php 对输入信息的进行安全过滤的函数代码
2012/06/29 PHP
PHP系列学习之日期函数使用介绍
2012/08/18 PHP
php实现中文字符截取防乱码方法汇总
2015/04/29 PHP
thinkPHP5项目中实现QQ第三方登录功能
2017/10/20 PHP
php实现的AES加密类定义与用法示例
2018/01/29 PHP
PHP使用file_get_contents发送http请求功能简单示例
2018/04/29 PHP
jquery.validate使用攻略 第一部
2010/07/01 Javascript
javascript新建标签,判断键盘输入,以及判断焦点(示例代码)
2013/11/25 Javascript
完美兼容各大浏览器的jQuery仿新浪图文淡入淡出间歇滚动特效
2014/11/12 Javascript
用JavaScript实现对话框的教程
2015/06/04 Javascript
jquery控制显示服务器生成的图片流
2015/08/04 Javascript
JS实现超简单的仿QQ折叠菜单效果
2015/09/21 Javascript
ion content 滚动到底部会遮住一部分视图的快速解决方法
2016/09/06 Javascript
node打造微信个人号机器人的方法示例
2018/04/26 Javascript
详解django模板与vue.js冲突问题
2019/07/07 Javascript
layer弹出层倒计时关闭的实现方法
2019/09/27 Javascript
Vuex的API文档说明详解
2020/02/05 Javascript
python 字符串split的用法分享
2013/03/23 Python
python matplotlib 注释文本箭头简单代码示例
2018/01/08 Python
详解Django+Uwsgi+Nginx 实现生产环境部署
2018/11/06 Python
PyCharm-错误-找不到指定文件python.exe的解决方法
2019/07/01 Python
Numpy(Pandas)删除全为零的列的方法
2020/09/11 Python
Python爬虫教程之利用正则表达式匹配网页内容
2020/12/08 Python
canvas 下载二维码和图片加水印的方法
2018/03/21 HTML / CSS
应届生护士求职信
2013/11/01 职场文书
自动化专业个人求职信范文
2013/12/30 职场文书
致200米运动员广播稿
2014/02/06 职场文书
小学英语课后反思
2014/04/26 职场文书
医学专业毕业生求职信
2014/06/20 职场文书
护士求职信
2014/07/05 职场文书
餐饮周年庆活动方案
2014/08/14 职场文书
2015年纪念“卢沟桥事变”78周年活动方案
2015/05/06 职场文书
JavaScript 对象创建的3种方法
2021/11/17 Javascript
php修改word的实例方法
2021/11/17 PHP
table设置超出部分隐藏,鼠标移上去显示全部内容的方法
2022/12/24 HTML / CSS