通过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 Csystemfunction,
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 subprocessmodule 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 thesubprocessdocumentation 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的更多相关文章

  1. Python调用外部程序

    通过os.system和subprocess.call()函数调用其他程序 预备知识:cmd中打开和关闭程序 cmd中打开程序 a.打开系统自带程序 系统自带的程序的路径一般都已加入环境变量之中,只需 ...

  2. Python——cmd调用(os.system阻塞处理)(多条命令执行)

    os.system(返回值为0,1,2)方法 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os ...

  3. Python——cmd调用(os.system阻塞处理)

    os.system(返回值为0,1,2) 0:成功 1:失败 2:错误 os.system默认阻塞当前程序执行,在cmd命令前加入start可不阻塞当前程序执行. 例如: import os os.s ...

  4. 调用系统命令 os.system()和os.popen()

    Python中os.system和os.popen区别 Python调用Shell,有两种方法:os.system(cmd)或os.popen(cmd)脚本执行过程中的输出内容.实际使用时视需求情况而 ...

  5. python基础之os.system函数

    前言 os.system方法是os模块最基础的方法,其它的方法一般在该方法基础上封装完成. os的system原理 system函数可以将字符串转化成命令在服务器上运行:其原理是每一条system函数 ...

  6. Python模块之OS,subprocess

    1.os 模块 简述: os 表示操作系统 该模块主要用来处理与系统相关操作 最常用的是文件操作 打开 获取 写入 删除 复制 重命名 常用操作 os.getcwd() : 返回当前文件所在文件夹路径 ...

  7. python os.system()返回值判断

    最近遇到os.system()执行系统命令的情况,上网搜集了一下资料,整理如下,以备不时之需,同时也希望能帮到某些人. 一.python中的 os.system(cmd)的返回值与linux命令返回值 ...

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

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

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

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

随机推荐

  1. thinkinginjava学习笔记08_接口

    抽象类和抽象方法 抽象方法是指没有具体实现的方法,仅仅有方法的声明和没有方法体:使用abstract关键字定义一个抽象方法:包含抽象方法的类成为抽象类,如果一个类中包含抽象方法则必须使用abstrac ...

  2. .Net WinForm 控件键盘消息处理剖析

    在WinForm控件上我们可以看到很多关于键盘消息处理的方法,比如OnKeyDown, OnKeyPress, ProcessCmdKey, ProcessDialogKey,IsInputKey等等 ...

  3. Kotlin——最详细的操作符与操作符重载详解(上)

    本篇文章为大家详细的介绍Koltin特有的操作符重载.或许对于有编程经验的朋友来说,操作符这个词绝对不陌生,就算没有任何编辑基础的朋友,数学中的算数运算符也绝不陌生.例如(+.-.*./.>.& ...

  4. Python 词云分析周杰伦《晴天》

    一.前言满天星辰的夜晚,他们相遇了...夏天的时候,她慢慢的接近他,关心他,为他付出一切:秋天的时候,两个人终於如愿的在一起,分享一切快乐的时光但终究是快乐时光短暂,因为杰伦必须出国深造,两人面临了要 ...

  5. Appium dmg 安装:[TypeError: Cannot read property 'replace' of undefined]

    问题原因:appium dmg 版本没有默认node.js 解决方案:安装稳定版的node.js.(官网下载安装即可.) 验证:命令行输入:node -v  查看版本号 npm -v  查看版本号

  6. [笔记]《JavaScript高级程序设计》- 最佳实践

    一.可维护性 1 什么是可维护的代码 可理解性--其他人可以接受代码并理解它的意图和一般途径,而无需原开发人员的完整解释. 直观性--代码中的东西一看就能明白,不管其操作过程多么复杂. 可适应性--代 ...

  7. JavaScript(五)语句

    js 的语句有 表达式语句,  复合语句{}, 空语句, 声明语句 if 默认不写大括号 可以执行 紧接着的一行 do-while do{}while() while for(初始化:判断:更新){执 ...

  8. Chris Richardson微服务翻译:微服务架构中的服务发现

    Chris Richardson 微服务系列翻译全7篇链接: 微服务介绍 构建微服务之使用API网关 构建微服务之微服务架构的进程通讯 微服务架构中的服务发现(本文) 微服务之事件驱动的数据管理 微服 ...

  9. 【读书笔记】【深入理解ES6】#13-用模块封装代码

    什么是模块 模块是自动运行在严格模式下并且没有办法退出运行的 JavaScript 代码. 在模块顶部创建的变量不会自动被添加到全局变量作用域,这个变量仅在模块的顶级作用域中存在,而且模块必须导出一些 ...

  10. windows 连接Linux

    服务器:阿里云 ecs 从 Windows 环境远程登录 Linux 实例 远程登录软件的用法大同小异.本文档以 Putty 为例,介绍如何远程登录实例.Putty 操作简单.免费.免安装, 下载地址 ...