subprocess
[root@wesley ~]# python Python 2.7.14 (default, Sep 10 2018, 23:42:00) [GCC 4.4.7 20120313 (Red Hat 4.4.7-23)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> >>> >>> import subprocess >>> ret = subprocess.call('ls -l',shell=True) #直接运行命令并打印 shell=True 表示程序将通过shell来执行 total 20 -rw-------. 1 root root 963 Jul 8 07:07 anaconda-ks.cfg -rw-r--r--. 1 root root 8845 Jul 8 07:07 install.log -rw-r--r--. 1 root root 3384 Jul 8 07:06 install.log.syslog >>> ret=subprocess.call('ls -l',shell=False) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/subprocess.py", line 168, in call return Popen(*popenargs, **kwargs).wait() File "/usr/local/lib/python2.7/subprocess.py", line 390, in __init__ errread, errwrite) File "/usr/local/lib/python2.7/subprocess.py", line 1025, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory >>> subprocess.check_call('ls -l',shell=True) #检查call 的执行命令,如果执行状态码是 0 ,则返回0,否则抛异常 total 20 -rw-------. 1 root root 963 Jul 8 07:07 anaconda-ks.cfg -rw-r--r--. 1 root root 8845 Jul 8 07:07 install.log -rw-r--r--. 1 root root 3384 Jul 8 07:06 install.log.syslog 0 >>> subprocess.check_call('exit 1',shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/subprocess.py", line 186, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command 'exit 1' returned non-zero exit status 1 >>> subprocess.check_output('echo','hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/subprocess.py", line 212, in check_output process = Popen(stdout=PIPE, *popenargs, **kwargs) File "/usr/local/lib/python2.7/subprocess.py", line 339, in __init__ raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer >>> subprocess.check_output(['echo','hello']) #args 是一个字符串或者元祖,列表(统一在某一个里面) 'hello\n' >>> subprocess.check_output('echo hello',shell=True) 'hello\n' >>> ret1=subprocess.Popen('mkdir t2',shell=True) >>> subprocee.Popen('ls -l',shell=True) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'subprocee' is not defined >>> subprocess.Popen('ls -l',shell=True) #Popen执行命令 <subprocess.Popen object at 0x7f5e8b43f850> >>> total 24 -rw-------. 1 root root 963 Jul 8 07:07 anaconda-ks.cfg -rw-r--r--. 1 root root 8845 Jul 8 07:07 install.log -rw-r--r--. 1 root root 3384 Jul 8 07:06 install.log.syslog drwxr-xr-x. 2 root root 4096 Oct 16 03:15 t2 >>> subprocess.Popen('ifconfig',shell=True) <subprocess.Popen object at 0x7f5e84f3dbd0> >>> eth0 Link encap:Ethernet HWaddr 00:0C:29:04:FE:91 inet addr:192.168.159.128 Bcast:192.168.159.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe04:fe91/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:1477 errors:0 dropped:0 overruns:0 frame:0 TX packets:715 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:129113 (126.0 KiB) TX bytes:87399 (85.3 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 (0.0 b) TX bytes:0 (0.0 b) >>> obj=subprocess.Popen('python',stdin=subprocess.PIPE, \
#在python 环境里面执行命令,
注意:如果希望通过进程的stdin向其发送数据,在创建Popen对象的时候,
参数stdin必须被设置为PIPE。同样,
如果希望从stdout和stderr获取数据,必须将stdout和stderr设置为PIPE。 ... stdout=subprocess.PIPE,stderr=subprocess.PIPE, \ ... universal_newlines=True) >>> obj.stdin.write('print(1)\n') >>> obj.stdin.write('print(2)') #写入数据 >>> 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) >>> obj=subprocess.Popen('python',stdin=subprocess.PIPE, \ ... stdout=subprocess.PIPE,stderr=subprocess.PIPE, \ ... universal_newlines=True) ... File "<stdin>", line 2 ... stdout=subprocess.PIPE,stderr=subprocess.PIPE, \ ^ SyntaxError: invalid syntax >>> File "<stdin>", line 1 ... universal_newlines=True) ^ SyntaxError: invalid syntax >>> import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) obj.stdin.write("print(1)\n") obj.stdin.write("print(2)") out_error_list = obj.communicate() #取数据 print(out_error_list)>>> >>> >>> >>> >>> >>> >>> ('1\n2\n', '') >>> import subprocess obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) out_error_list = obj.communicate('print("hello")') #写入并取数据 obj.communicate(input) print(out_error_list)>>> >>> >>> >>> ('hello\n', '') >>>