1.用python调用python脚本

  1. #!/usr/local/bin/python3.7
  2. import time
  3. import os
  4. count = 0
  5. str = ('python b.py')
  6. result1 = os.system(str)
  7. print(result1)
  8. while True:
  9. count = count + 1
  10. if count == 8:
  11. print('this count is:',count)
  12. break
  13. else:
  14. time.sleep(1)
  15. print('this count is:',count)
  16. print('Good Bye')

另外一个python脚本b.py如下:

  1. #!/usr/local/bin/python3.7
  2. print('hello world')

运行结果:

  1. [python@master2 while]$ python a.py
  2. hello world
  3. this count is: 1
  4. this count is: 2
  5. this count is: 3
  6. this count is: 4
  7. this count is: 5
  8. this count is: 6
  9. this count is: 7
  10. this count is: 8
  11. Good Bye

2.python调用shell方法os.system()

  1. #!/usr/local/bin/python3.7
  2. import time
  3. import os
  4. count = 0
  5. n = os.system('sh b.sh')
  6. while True:
  7. count = count + 1
  8. if count == 8:
  9. print('this count is:',count)
  10. break
  11. else:
  12. time.sleep(1)
  13. print('this count is:',count)
  14. print('Good Bye')

shell脚本如下:

  1. #!/bin/sh
  2. echo "hello world"

运行结果:

  1. [python@master2 while]$ python a.py
  2. hello world
  3. this count is: 1
  4. this count is: 2
  5. this count is: 3
  6. this count is: 4
  7. this count is: 5
  8. this count is: 6
  9. this count is: 7
  10. this count is: 8
  11. Good Bye

3.python调用shell方法os.popen()

  1. #!/usr/local/bin/python3.7
  2. import time
  3. import os
  4. count = 0
  5. n = os.system('sh b.sh')
  6. while True:
  7. count = count + 1
  8. if count == 8:
  9. print('this count is:',count)
  10. break
  11. else:
  12. time.sleep(1)
  13. print('this count is:',count)
  14. print('Good Bye')

运行结果:

  1. [python@master2 while]$ python a.py
  2. <os._wrap_close object at 0x7f7f89377940>
  3. ['hello world\n']
  4. this count is: 1
  5. this count is: 2
  6. this count is: 3
  7. this count is: 4
  8. this count is: 5
  9. this count is: 6
  10. this count is: 7
  11. this count is: 8
  12. Good Bye

os.system.popen() 这个方法会打开一个管道,返回结果是一个连接管道的文件对象,该文件对象的操作方法同open(),可以从该文件对象中读取返回结果。如果执行成功,不会返回状态码,如果执行失败,则会将错误信息输出到stdout,并返回一个空字符串。这里官方也表示subprocess模块已经实现了更为强大的subprocess.Popen()方法。

3种python调用其他脚本的方法,你还知道其他的方法吗?的更多相关文章

  1. Python 调用 Shell脚本的方法

    Python 调用 Shell脚本的方法 1.os模块的popen方法 通过 os.popen() 返回的是 file read 的对象,对其进行读取 read() 的操作可以看到执行的输出. > ...

  2. python调用其他脚本

    1.用python调用python脚本 #!/usr/local/bin/python3. import time import os count = str = ('python b.py') re ...

  3. python调用shell脚本时需要切换目录

    最近遇到了一个问题,就是python代码调用shell脚本时,发现输入输出的文件,总是和自己预想的有偏差,但是单独在linux下执行命令的时候,却没有错误.后来发现是相对路径的问题,因为执行pytho ...

  4. python调用shell脚本

    # coding=utf-8   //设置文本格式import os            //导入os方法print('hello')n=os.system('/home/csliyb/kjqy_x ...

  5. python调用matlab脚本

    在MATLAB和Python之间建个接口,从Python中调用MATLAB脚本或者是MATLAB的函数.内容不是很难,毕竟现成的接口已经有了,在这儿记录一下API使用的一些事项. 注:本篇使用的是MA ...

  6. 精华 selenium_webdriver(python)调用js脚本

    #coding=utf-8 from selenium import webdriver import time driver = webdriver.Firefox() driver.get(&qu ...

  7. python 调用java脚本的加密(没试过,先记录在此)

    http://lemfix.com/topics/344 前言 自动化测试应用越来越多了,尤其是接口自动化测试. 在接口测试数据传递方面,很多公司都会选择对请求数据进行加密处理. 而目前为主,大部分公 ...

  8. python 调用dll中c或c++语言的带指针方法,

    在项目开发中遇到了,python需要去调用一个动态链接库dll中的c++方法.这个方法的参数为一个指针类型的参数,一个bool类型参数, 在python中并未对数字类型进行区分. int LP_Agc ...

  9. 使用python调用其他脚本

    cmd = '<command line string>' print(cmd) p = subprocess.Popen(args=cmd, shell=True, stdout=sub ...

随机推荐

  1. UniGUI之ServerModule常用设置(07)

    主要有两个设置,它是一个单独的, 这意味着它只在每个应用程序中创建一次. 它主要用于配置各种服务器设置.不能将组件放在ServerModule上. 如前所述, ServerModule 是一个单一的, ...

  2. 查漏补缺之slice

    interviewer:说一说slice interviewee: 主要包括以下几点 slice and array slice的底层数据结构 length和capacity 切片的capacity的 ...

  3. ASP.NET MVC 4 中Razor 视图中JS无法调试 (重要)

    谷歌浏览器,firefox,IE 都可以 1.首先检查IE中这2个属性是否勾选了. 2.选择IE浏览器进行调试,调试方法有2种 A:采用debugger;的方法,如下图所示: 这时不用调试断点就会在d ...

  4. WC2020「Fantasie」

    由于某些不可抗拒因素,这篇文章鸽了

  5. NoNodeAvailableException异常的解决

    Elasticsearch 相关学习,昨天还好好的,今天就出错了!!! 完整异常为 : NoNodeAvailableException[None of the configured nodes ar ...

  6. vim的几种模式

    Normal Mode 普通模式 功能:在这种模式下可以移动光标等. 进入:默认进入vim之后,处于这种模式.在其他模式下狂按ESC后进入此模式. Visual Mode 可视模式 功能:在这种模式下 ...

  7. linux问题故障

    分析问题的方法论 What-现象是什么样的 When-什么时候发生 Why-为什么会发生 Where-哪个地方发生的问 How much-耗费了多少资源 How to do-怎么解决问题 4. cpu ...

  8. Xeon 第一次训练赛 苏州大学ICPC集训队新生赛第二场(同步赛) [Cloned]

    A.给出一个字符串,求出连续的权值递增和,断开以后权值重新计数,水题 #include<iostream> #include<string> #include<cmath ...

  9. vi/vim编辑器基本操作

    一.vi/vim的三种模式 vi编辑器有三种模式:命令模式(command mode).插入模式(Insert mode).底行模式(last line mode). 就是你直接用命令(vi  文件名 ...

  10. loadBeanDefinitions方法源码跟踪(二)

    因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/a0cfaedf3fc5 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 3.parseDef ...