简单了解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


猜你喜欢
- 之前在实现表单中file类型input选择多图片的时候找到一种方式 也许不是最好的但亲测可行且支持ie7以上以及chrome浏览器在表单中使
- time模块1:概述时间表示的分类时间戳格式化的时间字符串结构化时间时间戳:时间戳表示的是从1970年1月1日整0点到目前秒的偏移量,数据类
- 在项目中遇到后台数据还没有加载完毕,但是页面上调用了后台数据中的字段,这样就会报undefined。例如:一进入页面直接回显数据。我在cre
- 在页面层次结构,数据较多的时候,用v-if或者v-show就会出现div闪现,或者部分闪烁的结果。可以在根元素添加v-cloak来解决,并且
- 一、前言对于一个桌面应用来说,有时候单独一个窗口用户使用起来会不太方便,比方说写日报或者查看文件等,若是在同一窗口内,我只能做一件事,不能边
- js汉字简繁转换源代码:<html> <head> <title>汉字简繁转换工具_asp之家</
- 傅里叶变换图像处理一般分为空间域处理和频率域处理。空间域处理是直接对图像内的像素进行处理。空间域处理主要划分为灰度变换和空间滤波两种形式。灰
- 排序算法是《数据结构与算法》中最基本的算法之一,也是面试必背题,为方便技术交流,文末创建技术交流群。排序算法可以分为内部排序和外部排序,内部
- 导言:在前面的教程里我们探讨了如何为GridView控件添加radio buttons列。当用户最多只能选择一项数据时,我们可以在用户界面里
- 这个只是一个简单的比较无聊的尝试,类似blog等一些网站的换肤功能都是以此方式改变网站的皮肤。对于这些方法大家会的比较多,没啥说的。主要我是
- 一、属性的设置和获取1、属性的设置和获取主要有两种方式:<!DOCTYPE html><html lang="e
- 50个常用sql语句 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(
- 代码如下: <% '屏蔽主流的下载工具 Dimxurl,xtool '获取浏览器AGENT xurl=lcase(Re
- 想必每个DBA都喜欢挑战数据导入时间,用时越短工作效率越高,也充分的能够证明自己的实力。实际工作中有时候需要把大量数据导入数据库,然后用于各
- 使用到的库好多人都想了解一下如何对摄像头进行调用,然后进行目标检测,于是我做了这个小BLOG。opencv-python==4.1.2.30
- 首先:如果备份的数据库有两个文件,分别是.LDF 和 .MDF,打开企业管理器,在实例上右击---所有任务--附加数据库,然后选择那个.MD
- OK,今天我们来学习一下 python 中的日志模块,日志模块也是我们日后的开发工作中使用率很高的模块之一,接下来们就看一看今天具体要学习日
- 在蓝色看到的几个js日历代码,整理了一下。大家可以用在blog上,或者可以用在表单的日期类型的数据选择中,方便大家填写日期。1.动感的日历,
- 环境:python3.6 pyqt5只是简单的一个思路,请忽略脆弱的异常防护:# -*- coding: utf-8 -*-import s
- RFC文档有很多,有时候在没有联网的情况下也想翻阅,只能下载一份留存本地了。看了看地址列表,大概是这个范围:http://www.netwo