python调用其他脚本

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
0
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://www.cnblogs.com/hello-wei/p/10551044.html

时间: 2024-08-04 01:09:49

python调用其他脚本的相关文章

精华 selenium_webdriver(python)调用js脚本

#coding=utf-8 from selenium import webdriver import time driver = webdriver.Firefox() driver.get("https://www.baidu.com/") #给搜索输入框标红javascript脚本 js="var q=document.getElementById(\"kw\");q.style.border=\"2px solid red\";

python模拟鼠标键盘操作 GhostMouse tinytask 调用外部脚本或程序 autopy右键另存为

1.参考 autopy (实践见最后一章节) 用Python制作游戏外挂(上) AutoPy Introduction and Tutorial autopy.mouse.smooth_move(1, 1) 可以实现平滑移动 autopy - API Reference pip install PyUserInput SavinaRoja/PyUserInput [python3.5][PyUserInput]模拟鼠标和键盘模拟 Python-模拟鼠标键盘动作 autoit selenium借助

python学习之--调用shell脚本

python调用Shell脚本,有很多种方法,下面给出了三个python中执行shell命令的方法 第一种方案:os.system os.system返回脚本的退出状态码 现有一个shell脚本1.sh <span style="font-size:14px;"><span style="font-size:18px;"><span style="font-size:12px;">#!/bin/sh echo

python调用系统命令popen、system

python调用Shell脚本,有两种方法:os.system(cmd)或os.popen(cmd),前者返回值是脚本的退出状态码,后者的返回值是脚本执行过程中的输出内容.所以说一般我们认为popen更加强大 os.system(cmd): 该方法在调用完shell脚本后,返回一个16位的二进制 数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,即脚本中“exit 1”的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,则函数的返回值是0×100,换算为1

测试:python调用cmd命令三种方法

目前我使用到的python中执行cmd的方式有三种 使用os.system("cmd") 该方法在调用完shell脚本后,返回一个16位的二进制数,低位为杀死所调用脚本的信号号码,高位为脚本的退出状态码,即脚本中"exit 1"的代码执行后,os.system函数返回值的高位数则是1,如果低位数是0的情况下,则函数的返回值是0×100,换算为10进制得到256. 如果我们需要获得os.system的正确返回值,那使用位移运算可以还原返回值: >>>

python调用脚本或shell的方式

python调用脚本或shell有下面三种方式: os.system()特点:(1)可以调用脚本.(2)可以判断是否正确执行.(3)满足不了标准输出 && 错误 commands模块特点:(1). commands.getstatusoutput(cmd)用os.popen()执行命令cmd, 然后返回两个元素的元组(status, result). cmd执行的方式是{ cmd ; } 2&get;&1, 这样返回结果里面就会包含标准输出和标准错误.(2). comman

Zabbix调用外部脚本发送邮件:python编写脚本

Zabbix调用外部脚本发送邮件的时候,会在命令行传入两个参数,第一个参数就是要发送给哪个邮箱地址,第二个参数就是邮件信息,为了保证可以传入多个参数,所以假设有多个参数传入 #!/usr/bin/env python #encoding:utf8 # # Zabbix Server 发送邮件脚本 # from email import encoders from email.header import Header from email.mime.text import MIMEText fro

[Python]在python中调用shell脚本,并传入参数-02python操作shell实例

首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell_no_para.sh 内容如下:  test_shell_2_para.sh内容如下 注意含有变量的字符串要用 双引号 括起来 直接在命令行运行 test_shell_2_para.sh 执行结果如下: [email protected]348-G4:~$ sh test_shell_2_para

Python调用(运行)外部程序

在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32process模块中的函数.如果想进一步控制进程,则可以使用ctype模块,直接调用kernel32.dll中的函数. 1 使用os.system函数运行其他程序2 使用ShellExecute函数运行其他程序3 使用CreateProcess函数运行其他程序4 使用ctypes调用kernel32.dl