Python调用外部程序——os.system()和subprocess.call-程序员宅基地

技术标签: python  shell  操作系统  

通过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 后回车即可关闭网易云音乐。

如下图所示:

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

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的区别以后补充。

转载于:https://www.cnblogs.com/kyrin/p/5967622.html

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/ailsa0503/article/details/102252393

智能推荐

利用KNN算法绘画PR曲线和ROC曲线_knn可以做roc曲线吗-程序员宅基地

文章浏览阅读630次。利用KNN算法_knn可以做roc曲线吗

docker加载镜像报错 dockerError processing tar file(exit status 1): no space left on device-程序员宅基地

文章浏览阅读1.7w次,点赞6次,收藏31次。问题描述: 在运行docker load 加载镜像时报错 dockerError processing tar file(exit status 1): no space left on device问题分析: 造成以上报错的原因是因为docker加载镜像的空间不足了解决办法:第一个解决办法查询到docker默认存放镜像地址为 /var/lib/docker ,扩展此地址下的空间就可以解决这个问题,但是博主并没使用这个方法。第二个解决办法修改 docker root pathstep 1_processing tar file

node上传word转html,NodeJs之word文件生成与解析的实现代码-程序员宅基地

文章浏览阅读1.4k次。一,介绍与需求1.1,介绍1, officegen模块可以为Microsoft Office 2007及更高版本生成Office Open XML文件。此模块不依赖于任何框架,您不需要安装Microsoft Office,因此您可以将它用于任何类型的JavaScript应用程序。输出也是流而不是文件,不依赖于任何输出工具。此模块应适用于支持Node.js 0.10或更高版本的任何环境,包括Li..._node word转html

Android环境搭建_ac8x_demo-程序员宅基地

文章浏览阅读434次。An参考:Android9-版本Android 镜像使用帮助Android源码下载-编译_ac8x_demo

module.export跟exports的区别_export和exports区别-程序员宅基地

文章浏览阅读1k次,点赞3次,收藏2次。module.export跟exports的区别返回的数据类型module.exports 方法还可以单独返回一个数据类型(String、Number、Object…),而 exports 只能返回一个 Object 对象职能不同所有的 exports 对象最终都是通过 module.exports 传递执行,因此可以更确切地说,exports 是给 module.exports 添加属性..._export和exports区别

【VSCode】【msys2】VS Code + msys2配置Windows下C/C++开发环境_vscode msys-程序员宅基地

文章浏览阅读5.1k次。在.vscode目录下新建一个json文件:c_cpp_properties.json,注意includePath和compilerPath要指定到msys2安装目录下。还有个问题,就是VSCode显示#include 这一行有错,鼠标移上去显式找不到依赖文件stddef.h。把这个目录也添加到c_cpp_properties.json的includePath中,问题解决。原因是VSCode做代码分析的时候不知道gcc,选择了MSVC,添加配置文件把编译器改为gcc.愉快的coding!_vscode msys

随便推点

c语言数据结构——链表的实现及其基本操作_编写完整程序设计算法:实现线性表中链表的各种基本操作,至少包含初始化,创建,输出-程序员宅基地

文章浏览阅读3.2k次,点赞46次,收藏57次。顺序表的问题及思考中间/头部的插入删除,时间复杂度为O(N)增容需要申请新空间,拷贝数据,释放旧空间。会有不小的消耗。增容一般是呈2倍的增长,势必会有一定的空间浪费。例如当前容量为100,满了以后增容到200,我们再继续插入了5个数据,后面没有数据插入了,那么就浪费了95个数据空间。思考:如何解决以上问题呢?下面我们给出了链表的结构,让我们来看看吧。_编写完整程序设计算法:实现线性表中链表的各种基本操作,至少包含初始化,创建,输出

json转list遇到的坑 com.alibaba.fastjson.JSONException: unclosed string : w-程序员宅基地

文章浏览阅读5.7w次。转换的工具类我是使用的阿里的com.alibaba.fastjson.JSONArray;操作如下:List&lt;XXX&gt; list =JSONArray.parseArray(XXX.get("json").toString().trim(), XXX.class); 在这一步时,json转list报了错, :com.alibaba.fastjson.JSONExce..._unclosed string : w

Web2.0设计师工具箱,国外的一些网站制作资源,css,javascript,ajax,设计素材等_web.js-vip.site-程序员宅基地

文章浏览阅读1.4w次。http://hi.baidu.com/sw%5Fws/blog/item/32b4237e63bbf13b0cd7da83.html Web2.0工具箱 The Web Designers Tool Kit原文地址: http://www.dezinerfolio.com/2007 ... designers-tool-kit/分类一、DHTML AJAX Javascrip_web.js-vip.site

使用属性自定义Ribbon配置_ribbon中自定义属性-程序员宅基地

文章浏览阅读2k次。一 介绍 从版本1.2.0开始,Spring Cloud支持使用属性来自定义Ribbon客户端。这种方式比使用Java代码配置的方式更方便。支持的属性如下,应以&lt;clientName&gt;.ribbon.为前缀:NFLoadBalancerClassName:应配置ILoadBalancerNFLoadBalancerRuleClassName:应配置IRuleNFLoadBalancer..._ribbon中自定义属性

java-netty知识点笔记和注意事项-程序员宅基地

文章浏览阅读486次,点赞6次,收藏7次。java-netty知识点笔记和注意事项

小样本学习----半监督学习算法-程序员宅基地

文章浏览阅读1.8k次。https://blog.csdn.net/mao_feng/article/details/78939864现实生活中,我们会遇到少量有标签的样本,而大量无标签的样本,怎么去做这个处理呢?方法1:迁移学习的finetune找类似的通用数据集(在图像领域:imagenet,电商领域:淘宝电商数据)训练网络,通过修改后面2层或者3层网络,做迁移学习,来微调网络的参数,从而...

推荐文章

热门文章

相关标签