通过os.system和subprocess.call()函数调用其他程序

预备知识:cmd中打开和关闭程序

cmd中打开程序

a.打开系统自带程序

系统自带的程序的路径一般都已加入环境变量之中,只需在cmd窗口中直接输入程序名称即可。

以notepad为例,直接在cmd窗口中输入notepad后回车即可打开。

b.打开自己安装且没有加入环境变量的程序

以网易云音乐为例,在cmd窗口中需要输入完整的程序路径  "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"。

注意,双引号是必须的。

若将网易云音乐的路径加入环境变量之中,则在cmd窗口中输入cloudmusic后回车即可运行。

在cmd中关闭程序

在cmd中关闭程序可以使用taskkill命令,语法如下:

taskkill /f /t /im 进程名

注意,这里只需要程序的进程名即可,而非完整路径名。

仍以网易云音乐为例,在cmd窗口中输入 taskkill /f /t /im cloudmusic.exe 后回车即可关闭网易云音乐。

如下图所示:

a.os.system方法

os.system(command)  链接 https://docs.python.org/2/library/os.html#os.system

Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variableCOMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocessdocumentation for some helpful recipes.

Availability: Unix, Windows.

os模块中的system()函数可以方便地运行其他程序或者脚本。其函数原型为:
os.system(command)
command 为要执行的命令,近似于Windows下cmd窗口中输入的命令。

如果要向程序或者脚本传递参数,可以使用空格分隔程序及多个参数。

b.用subprocess.call()代替os.system()

17.1.4.3. Replacing os.system()    

链接 https://docs.python.org/2/library/subprocess.html#replacing-os-system

  1. 1 status = os.system("mycmd" + " myarg")
  2. 2 # becomes
  3. 3 status = subprocess.call("mycmd" + " myarg", shell=True)

Notes:

  • Calling the program through the shell is usually not required.

A more realistic example would look like this:

  1. 1 try:
  2. 2 retcode = call("mycmd" + " myarg", shell=True)
  3. 3 if retcode < 0:
  4. 4 print >>sys.stderr, "Child was terminated by signal", -retcode
  5. 5 else:
  6. 6 print >>sys.stderr, "Child returned", retcode
  7. 7 except OSError as e:
  8. 8 print >>sys.stderr, "Execution failed:", e

实例演示:

打开记事本:

  1. 1 import os
  2. 2 os.system('notepad')

  1. 1 import subprocess
  2. 2 subprocess.call('notepad')

我们看以下代码:

  1. 1 import os
  2. 2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"')

这段代码会启动网易云音乐,效果和我们在cmd窗口中输入 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe" 效果一样。注意字符串中含有空格,所以有 r''。

而以下代码也可以实现同样的功能:

  1. 1 import subprocess
  2. 2 subprocess.call("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe")

同上面那段代码的区别只是括号中的 r''。

到目前为止一切正常,我们再看下面的代码,尝试着同时打开两个程序:

  1. 1 import os
  2. 2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"')
  3. 3
  4. 4 os.system("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad")
  5. 5
  6. 6 os.system(""D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"")

以上尝试都不会成功。

换做subprocess.call()函数也不能实现。

这个问题早在07年就有人提交过,请参考http://bugs.python.org/issue1524

os.system()和subprocess.call()的区别以后补充。

  1.  

Python调用外部程序的更多相关文章

  1. Python调用外部程序——os.system()和subprocess.call

    通过os.system函数调用其他程序 预备知识:cmd中打开和关闭程序 cmd中打开程序 a.打开系统自带程序 系统自带的程序的路径一般都已加入环境变量之中,只需在cmd窗口中直接输入程序名称即可. ...

  2. Python调用(运行)外部程序

    在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...

  3. 【转】 Python调用(运行)外部程序

    在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...

  4. 举例讲解Linux系统下Python调用系统Shell的方法

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法:1. os 模块 ...

  5. python调用linux的命令

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...

  6. python 调用Linux shell

    有时候难免需要直接调用Shell命令来完成一些比较简单的操作,比如mount一个文件系统之类的.那么我们使用Python如何调用Linux的Shell命令?下面来介绍几种常用的方法: 1. os 模块 ...

  7. [教程]K8Cscan调用外部程序(Win/Linux批量上控/执行多条命令/保存结果)

    0x000 调用原理 Cscan调用外部程序有两种方式,一是编写DLL,二是配置文件 编写DLL文件对于不懂编程的人来说可能会很难(虽然支持各语言) 由于考虑到很多人不会编程或会编程又急用无法短时间转 ...

  8. 笔记||Python3进阶之调用外部程序

    像wget可以下载文件 ffmpeg可以切割.合并.转换.录制视频 free命令可以查看linux内存使用信息 python提供了库来调用外部程序.命令?> 最常见的两种方法:       ①o ...

  9. 【初学python】使用python调用monkey测试

    目前公司主要开发安卓平台的APP,平时测试经常需要使用monkey测试,所以尝试了下用python调用monkey,代码如下: import os apk = {'j': 'com.***.test1 ...

随机推荐

  1. 基于RxJava2+Retrofit2简单易用的网络请求实现

    代码地址如下:http://www.demodashi.com/demo/13473.html 简介 基于RxJava2+Retrofit2实现简单易用的网络请求,结合android平台特性的网络封装 ...

  2. mongodb安装的两条命令

    1. 安装 下载并安装,注意安装方式为custom,路径自定义(d:\chengxu\mongodb),安装成功后在mongodb文件夹下新建data文件夹(内新建db文件夹)和logs文件夹(内新建 ...

  3. 转:浅析VO、DTO、DO、PO的概念、区别和用处

    原文链接 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对象,这个概 ...

  4. ExtJS学习------Ext.define的继承extend,用javascript实现相似Ext的继承

    (1)Ext.define的继承extend 详细实例: Ext.onReady(function(){ //Sup Class 父类 Ext.define('Person',{ config:{ n ...

  5. Vue 中的组件

    VUE中的组件 一个自定义的标签,vue就会把他看成一个组件,vue可以给这些标签赋予一定意义:一个页面就是一个组件 好处: 1.提高开发效率 2.方便重复使用 3.便于协同开发 4.更容易被管理和维 ...

  6. CentOS6.6+Puppet3.7.4分布式部署Nagios监控系统

    测试框架 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 CentOS-6.6-x86_64(minimal)   puppet-3.7 ...

  7. atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx

    atitit..国富论 在现代it企业项目管理中的作用attialx 总结---国富论读后感 attialx 1. 国民财富的性质和原因的研究(简称:<国富论>) 1 2. 蕴含的重要管理 ...

  8. Android开发-状态栏着色原理和API版本号兼容处理

    介绍 先上实际效果图,有三个版本号请注意区分API版本号 API>=20 API=19 API<19 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZX ...

  9. 221. Add Two Numbers II【medium】

    You have two numbers represented by a linked list, where each node contains a single digit. The digi ...

  10. python对象序列化之pickle

    本片文章主要是对pickle官网的阅读记录. The pickle module implements binary protocols for serializing and de-serializ ...