当前第2页 返回上一页
执行命令,如果状态码是 0 ,则返回执行结果,否则抛异常
1 2 3 4 5 6 7 8 9 10 | >>> 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),它将在子进程运行之前被调用
close_sfs:在windows平台下,如果close_fds被设置为True,则新创建的子进程将不会继承父进程的输入、输出、错误管道。
所以不能将close_fds设置为True同时重定向子进程的标准输入、输出与错误(stdin, stdout, stderr)。
shell:同上
cwd:用于设置子进程的当前目录
env:用于指定子进程的环境变量。如果env = None,子进程的环境变量将从父进程中继承。
universal_newlines:不同系统的换行符不同,True -> 同意使用 n
startupinfo与createionflags只在windows下有效
将被传递给底层的CreateProcess()函数,用于设置子进程的一些属性,如:主窗口的外观,进程的优先级等等
执行普通命令
1 2 3 4 5 6 7 | >>> import subprocess
>>> ret1 = subprocess.Popen(["mkdir","t1"])
>>> ret2 = subprocess.Popen("mkdir t2", shell=True)
>>> print(ret1)
< subprocess.Popen object at 0x7f4d7609dd30>
>>> print(ret2)
< subprocess.Popen object at 0x7f4d7609dc18>
|
终端输入的命令分为两种:
输入即可得到输出,如:ifconfig
输入进行某环境,依赖再输入,如:python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | >>> import subprocess
>>> obj = subprocess.Popen("mkdir t3", shell=True, cwd='/tmp/',)
>>> import subprocess
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> obj.stdin.write("print(1)\n")
9
>>> obj.stdin.write("print(2)")
8
>>> obj.stdin.close()
>>> cmd_out = obj.stdout.read()
>>> obj.stdout.close()
>>> cmd_error = obj.stderr.read()
>>> obj.stderr.close()
>>> print(cmd_out)
1
2
>>> print(cmd_error)
|
1 2 3 4 5 6 7 8 9 10 11 | >>> import subprocess
>>>
>>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> obj.stdin.write("print(1)\n")
9
>>> obj.stdin.write("print(2)")
8
>>>
>>> out_error_list = obj.communicate()
>>> print(out_error_list)
('1\n2\n', '')
|
1 2 3 4 | >>> obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
>>> out_error_list = obj.communicate('print("hello")')
>>> print(out_error_list)
('hello\n', '')
|
相关推荐:
利用python执行shell脚本 并动态传参 及subprocess基本使用
subprocess 模块的介绍与使用
Python标准库子进程subprocess包的详细介绍
以上就是python中subprocess批量执行linux命令的详细内容,更多文章请关注木庄网络博客!!
返回前面的内容
相关阅读 >>
Python最长回文串算法
如何安装Python包
Python编程ide是什么
selenium之实现自动登录的实例代码
Python e怎么表示
Python如何实现无限循环
Python模块的编写与使用(实例解析)
如何在debian 9上安装Python 3.7?
Python如何安装urllib2库
pyenv管理多个版本Python环境的详解
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中subprocess批量执行linux命令