可以执行shell命令的相关模块和函数有:
os.system os.spawn os.popen --废弃 popen --废弃 commands --废弃,3.x中被移除以上执行shell命令的相关的模块和函数的功能均在 subprocess 模块中实现,并提供了更丰富的功能。
subprocess
call
执行命令,返回状态码
>>> import subprocess>>> ret = subprocess.call(["ls", "-l"], shell=False)total 4684-rw-r--r-- 1 root root 454 May 5 12:20 aa.py-rw-r--r-- 1 root root 0 May 8 16:51 aa.txt-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe-rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt-rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf>>> ret = subprocess.call("ls -l", shell=True)total 4684-rw-r--r-- 1 root root 454 May 5 12:20 aa.py-rw-r--r-- 1 root root 0 May 8 16:51 aa.txt-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe-rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt-rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf>>> print(ret)0
check_call
执行命令,如果执行状态码是 0 ,则返回0,否则抛异常
>>> subprocess.check_call(["ls", "-l"])total 4684-rw-r--r-- 1 root root 454 May 5 12:20 aa.py-rw-r--r-- 1 root root 0 May 8 16:51 aa.txt-rw-r--r-- 1 root root 4783286 Apr 11 16:39 DockerToolbox.exe-rw-r--r-- 1 root root 422 May 5 12:20 ip_info.txt-rw-r--r-- 1 root root 718 Apr 19 10:52 my.cnf0>>> subprocess.check_call("exit 1", shell=True)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 581, in check_call raise CalledProcessError(retcode, cmd)subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
check_output
执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常
>>> subprocess.check_output(["echo", "Hello World!"])b'Hello World!/n'>>> subprocess.check_output("exit 1", shell=True)Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 626, in check_output **kwargs).stdout File "/usr/local/python3.5/lib/python3.5/subprocess.py", line 708, in run output=stdout, stderr=stderr)subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1
subprocess.Popen(...)
用于执行复杂的系统命令
参数:
args:shell命令,可以是字符串或者序列类型(如:list,元组)
bufsize:指定缓冲。0 无缓冲,1 行缓冲,其他 缓冲区大小,负值 系统缓冲
stdin, stdout, stderr:分别表示程序的标准输入、输出、错误句柄
preexec_fn:只在Unix平台下有效,用于指定一个可执行对象(callable object),它将在子进程运行之前被调用
新闻热点
疑难解答