通过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 status = os.system("mycmd" + " myarg")
2 # becomes
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 try:
2     retcode = call("mycmd" + " myarg", shell=True)
3     if retcode < 0:
4         print >>sys.stderr, "Child was terminated by signal", -retcode
5     else:
6         print >>sys.stderr, "Child returned", retcode
7 except OSError as e:
8     print >>sys.stderr, "Execution failed:", e

 实例演示:

打开记事本:

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

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

我们看以下代码:

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

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

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

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

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

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

1 import os
2 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()的区别以后补充。

转载于:https://www.cnblogs.com/songwenlong/p/5940155.html

Python调用外部程序——os.system()和subprocess.call()相关推荐

  1. python调用外部程序 退出_Python调用外部程序——os.system()和subprocess.call

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

  2. python调用程序call_Python调用外部程序——os.system()和subprocess.call()

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

  3. python os system用法_Python调用系统命令os.system()和os.popen()的实现

    作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法. os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和pope ...

  4. python中os system_Python调用系统命令os.system()和os.popen()的实现

    作为一门脚本语言,写脚本时执行系统命令可以说很常见了,python提供了相关的模块和方法. os模块提供了访问操作系统服务的功能,由于涉及到操作系统,它包含的内容比较多,这里只说system和pope ...

  5. python执行系统命令的方法_python执行系统命令的方法 :os.system(),subprocess.popen(),command...

    最近接触到os.system(),subprocess.popen()和commands来执行系统命令,从网上搜索到许多,现整理如下. 1. 使用os.system("cmd") ...

  6. Python中 os.popen、os.system和subprocess.popen方法介绍

    Python 提供了多种与操作系统交互的方法,比如os模块中的popen和system方法,此外,Python subprocess模块中的Popen类也提供了与操作系统交互的方法,使用起来更加灵活, ...

  7. python调用程序call_Python下的subprocess.call()使用和注意事项

    Python虽然有许多优秀的第三方库,但在实际使用的时候免不了使用一些cmd调用的程序,毕竟这类程序比较底层,更快.也更稳定.比如GDAL.FFmpeg. ImageMagick等. 利用Python ...

  8. python调用外部程序 退出_Python调用(运行)外部程序

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

  9. 与 python 中的 os.system(cmd) 返回值与linux 命令返回值对应关系

    一.python中的 os.system(cmd)的返回值与linux命令返回值(具体参见本文附加内容)的关系 大家都习惯用os.systemv()函数执行linux命令,该函数的返回值十进制数(分别 ...

最新文章

  1. Apache Tiles 学习(四)、Tiles实战
  2. Windows Phone 7 cs页面添加样式
  3. 39. Combination Sum
  4. vb获取textbox数字_Spectrum仪器PCIe数字化仪可额外扩展8个数字输入
  5. Cookie和Session简介与区别
  6. kubernetes之CI/CD工具jenkins第二篇,helm的使用
  7. python怎么批量处理数据_python如何批量处理excel数据?_后端开发
  8. 95后夜猫子报告:三成熬夜到1点 夜间最爱追《乡村爱情》
  9. 监控Nginx负载均衡器脚本
  10. 看我如何挖到 Dropbox Windows 版的这个 0day(微补丁发布)
  11. 2020-11-25 阿里云CentOS linux源配置脚本 https://mirrors.aliyun.com/repo/
  12. IE11的userAgent
  13. 如果看了此文你还不懂傅里叶变换,那就过来掐死我吧【完整版】
  14. JAVA中interface接口的使用
  15. 第四章-2 hydra(九头蛇)与Medusa(美杜莎)
  16. 六年一轮回:大数据改变的,不仅仅是我的专业!
  17. oracle中的||是什么意思?
  18. 计算机显示屏对比度怎么调整,如何调节电脑屏幕亮度(电脑如何调节显示器亮度和对比度?)...
  19. Tracup|10个有效的工作习惯,成功的例子和技巧
  20. 国内的虚拟服务器推荐,虚拟空间哪个好(国内比较的几款虚拟主机推荐)

热门文章

  1. 学习笔记之yum的本地源配置和ATP简介和使用
  2. SSH协议、HTTPS中SSL协议的完整交互过程
  3. iOS 隐藏导航栏整个视图上移闪屏问题
  4. 最详细的JavaWeb开发基础之java环境搭建(Windows版)
  5. MySQL数据类型以及元数据的使用
  6. 廖雪峰JS教程学习记录---字符串
  7. 什么是 Delta 文件
  8. 项目中基于Rest的Wcf服务发布以及iBatisNet框架的使用(下)
  9. IT餐馆—第二回 私活
  10. Scala进阶之路-正则表达式案例