Python调用外部程序——os.system()和subprocess.call
通过os.system函数调用其他程序
预备知识: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方法
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system
, and has the same limitations. Changes tosys.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 Csystem
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 runningcommand, given by the Windows environment variableCOMSPEC
: oncommand.comsystems (Windows 95, 98 and ME) this is always0
;
oncmd.exesystems (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 theReplacing Older Functions with the subprocess
Modulesection in thesubprocess
documentation for some helpful recipes.
Availability: Unix, Windows.
os模块中的system函数可以方便地运行其他程序或者脚本。其函数原型为:
os.system(command)
command 为要执行的命令,近似于Windows下cmd窗口中输入的命令。
如果要向程序或者脚本传递参数,可以使用空格分隔程序及多个参数。
1 status = os.system("mycmd" + " myarg")2 # becomes3 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 try:2 retcode = call("mycmd" + " myarg", shell=True)3 if retcode < 0:4 print >>sys.stderr, "Child was terminated by signal", -retcode5 else:6 print >>sys.stderr, "Child returned", retcode7 except OSError as e:8 print >>sys.stderr, "Execution failed:", e
实例演示:
打开记事本:
1 import os2 os.system('notepad')
或
1 import subprocess2 subprocess.call('notepad')
我们看以下代码:
1 import os2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"')
这段代码会启动网易云音乐,效果和我们在cmd窗口中输入 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe" 效果一样。注意字符串中含有空格,所以有 r''。
而以下代码也可以实现同样的功能:
1 import subprocess2 subprocess.call("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe")
同上面那段代码的区别只是括号中的 r''。
到目前为止一切正常,我们再看下面的代码,尝试着同时打开两个程序:
1 import os2 os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"')3 或4 os.system("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad")5 或6 os.system(""D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"")
以上尝试都不会成功。
换做subprocess.call函数也不能实现。
这个问题早在07年就有人提交过,请参考http://bugs.python.org/issue1524
os.system和subprocess.call的区别以后补充。
Python调用外部程序——os.system()和subprocess.call的更多相关文章
- Python调用外部程序
通过os.system和subprocess.call()函数调用其他程序 预备知识:cmd中打开和关闭程序 cmd中打开程序 a.打开系统自带程序 系统自带的程序的路径一般都已加入环境变量之中,只需 ...
- Python——cmd调用(os.system阻塞处理)(多条命令执行)
os.system(返回值为0,1,2)方法 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os ...
- Python——cmd调用(os.system阻塞处理)
os.system(返回值为0,1,2) 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os.s ...
- 调用系统命令 os.system()和os.popen()
Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...
- python基础之os.system函数
前言 os.system方法是os模块最基础的方法,其它的方法一般在该方法基础上封装完成. os的system原理 system函数可以将字符串转化成命令在服务器上运行:其原理是每一条system函数 ...
- Python模块之OS,subprocess
1.os 模块 简述: os 表示操作系统 该模块主要用来处理与系统相关操作 最常用的是文件操作 打开 获取 写入 删除 复制 重命名 常用操作 os.getcwd() : 返回当前文件所在文件夹路径 ...
- python os.system()返回值判断
最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...
- Python调用(运行)外部程序
在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...
- 【转】 Python调用(运行)外部程序
在Python中可以方便地使用os模块运行其他的脚本或者程序,这样就可以在脚本中直接使用其他脚本,或者程序提供的功能,而不必再次编写实现该功能的代码.为了更好地控制运行的进程,可以使用win32pro ...
随机推荐
- dotnet core 自定义配置文件
首先添加一个.json 文件,比如 setting.json 文件内容如下,记得把文件设置为“复制到输出目录” { "ConfigSetting": { "XXXName ...
- [Spark内核] 第36课:TaskScheduler内幕天机解密:Spark shell案例运行日志详解、TaskScheduler和SchedulerBackend、FIFO与FAIR、Task运行时本地性算法详解等
本課主題 通过 Spark-shell 窥探程序运行时的状况 TaskScheduler 与 SchedulerBackend 之间的关系 FIFO 与 FAIR 两种调度模式彻底解密 Task 数据 ...
- Django-环境搭建
django开发环境搭建 环境说明 python django mysql pymysql pycharm 安装过程 安装python3.6.3 64位下载地址: https://www.python ...
- iOS 动画篇 (三) CADisplayLink与CoreGraphics实现动画
本文主要介绍利用CoreGraphics和CADisplayLink来实现一个注水动画.来一个效果图先: 在介绍注水动画前,先介绍利用CoreGraphics实现进度条的绘制. 一.扇形进度绘制 效果 ...
- spring 实现邮箱发送
使用spring mail 实现的邮箱发送功能,包括附件的发送(附件发送要保证附件存在的路径是真实),使用maven集成jar包,通过spring mvc 实现前后台的调用,发送方使用的是163邮箱, ...
- Java集合干货——LinkedList源码分析
前言 在上篇文章中我们对ArrayList对了详细的分析,今天我们来说一说LinkedList.他们之间有什么区别呢?最大的区别就是底层数据结构的实现不一样,ArrayList是数组实现的(具体看上一 ...
- 修复Java使用POI合并Excel单元格后,边框不显示的问题
使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决. 创建方法: public void setBorderStyle(int bor ...
- python编码问题和逻辑运算
1,回顾昨天课程及作业 #1.使用while循环输入 1 2 3 4 5 6 8 9 10 ''' count = 0 while count < 10: count += 1 # count ...
- Java与算法之(6) - 八皇后问题
在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于同一行.同一列或同一斜线上,问有多少种摆法. (文字和图片来自百度百科) 如果动手来摆放皇后,可以用这样一种思路:在最左侧 ...
- linux(五)之vi编译器
前面介绍了linux的常用命令和对文本的操作,接下来我将对大家领略一下vi编译器的强大功能.希望大家觉得写的还不错的话可以点个“推荐”哦! 一.vim/vi编译器简介 Vim/Vi是一个功能强大的全屏 ...