您的位置:首页 > 编程语言 > Python开发

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

2016-10-08 23:41 459 查看

通过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) [b]链接 https://docs.python.org/2/library/os.html#os.system
[/b]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 variable
COMSPEC
: 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
subprocess
documentation 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

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

实例演示:

打开记事本:
import os
os.system('notepad')
import subprocess
subprocess.call('notepad')
我们看以下代码:
import os
os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe"')
这段代码会启动网易云音乐,效果和我们在cmd窗口中输入 "D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe" 效果一样。注意字符串中含有空格,所以有 r''。而以下代码也可以实现同样的功能:
import subprocess
subprocess.call("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe")
同上面那段代码的区别只是括号中的 r''。到目前为止一切正常,我们再看下面的代码,尝试着同时打开两个程序:
import os
os.system(r'"D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"')
或
os.system("D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad")
或
os.system(""D:\Program Files (x86)\Netease\CloudMusic\cloudmusic.exe""notepad"")
以上尝试都不会成功。换做subprocess.call()函数也不能实现。这个问题早在07年就有人提交过,请参考http://bugs.python.org/issue1524os.system()和subprocess.call()的区别以后补充。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐