popen

os.system

执行的命令为打印,返回值为0或1

>>> a=os.system('ls')
usage
0
>>> 
>>> a   #a的返回值为命令状态
0
>>> 

os.popen

可以保存返回值,无法保存返回值

>>> a=os.popen('df -h')
>>> 
>>> a.read()
'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n'
>>> 

 

subprocess

subprocess.run

>>> a=subprocess.run(['df','-h'],stderr=subprocess.PIPE,stdout=subprocess.PIPE,check=True)
>>> 
>>> 
>>> a
CompletedProcess(args=['df', '-h'], returncode=0, stdout=b'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n', stderr=b'')
>>> 
>>> 

涉及到管道 用shell=True,此时返回值returncode=0

>>> a=subprocess.run('df -h | grep dev',shell=True)
udev            918M     0  918M   0% /dev
/dev/vda1        50G  2.4G   45G   6% /
tmpfs           937M   24K  937M   1% /dev/shm
>>> 
>>> a
CompletedProcess(args='df -h | grep dev', returncode=0)
>>> 
>>> a=subprocess.run(['df','-h']) 
Filesystem      Size  Used Avail Use% Mounted on
udev            918M     0  918M   0% /dev
tmpfs           188M   20M  168M  11% /run
/dev/vda1        50G  2.4G   45G   6% /
tmpfs           937M   24K  937M   1% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           937M     0  937M   0% /sys/fs/cgroup
tmpfs           188M     0  188M   0% /run/user/1000
>>> 
>>> 
>>> a
CompletedProcess(args=['df', '-h'], returncode=0)
>>> 
>>> 

即不用列表形式,需要指定shell=True。

 stdout与stderr ,是否shell=True不影响

>>> a=subprocess.run(['df','-h'],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a
CompletedProcess(args=['df', '-h'], returncode=0, stdout=b'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n', stderr=b'')
>>> 
>>> 
>>> 
>>> a.stdout
b'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n'
>>> 
>>> 




>>> a=subprocess.run('df -h',stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> a
CompletedProcess(args='df -h', returncode=0, stdout=b'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n', stderr=b'')

 subprocess.getstatusoutput

>>> a=subprocess.getstatusoutput('df -h')
>>> a
(0, 'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000')
>>> 

 

 subprocess.Popen

 比较

 run方法阻塞10秒,  Popen非阻塞,可以用poll来查看是否执行完,

>>> a=subprocess.run('sleep 10',stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)
>>> 
>>> a=subprocess.Popen('sleep 10',stdout=subprocess.PIPE,stderr=subprocess.PIPE,shell=True)   
>>> a.poll()
>>> 
>>> a.poll()
>>> 
>>> a.poll()
0
>>> 

获取结果方法发生改变

>>> a=subprocess.Popen('df -h',shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> 
>>> 
>>> a.stdout.read()
b'Filesystem      Size  Used Avail Use% Mounted on\nudev            918M     0  918M   0% /dev\ntmpfs           188M   20M  168M  11% /run\n/dev/vda1        50G  2.4G   45G   6% /\ntmpfs           937M   24K  937M   1% /dev/shm\ntmpfs           5.0M     0  5.0M   0% /run/lock\ntmpfs           937M     0  937M   0% /sys/fs/cgroup\ntmpfs           188M     0  188M   0% /run/user/1000\n'
>>> 
>>> dir(a)
['__class__', '__del__', '__delattr__', '__dict__', '__dir__', '__doc__', '__enter__', '__eq__', '__exit__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_check_timeout', '_child_created', '_closed_child_pipe_fds', '_communicate', '_communication_started', '_execute_child', '_get_devnull', '_get_handles', '_handle_exitstatus', '_input', '_internal_poll', '_remaining_time', '_save_input', '_stdin_write', '_translate_newlines', '_try_wait', '_waitpid_lock', 'args', 'communicate', 'kill', 'pid', 'poll', 'returncode', 'send_signal', 'stderr', 'stdin', 'stdout', 'terminate', 'universal_newlines', 'wait']
>>> 

 

设置cwd

 

>>> a=subprocess.Popen('echo $PWD',shell=True,cwd='/tmp',stdout=subprocess.PIPE,stderr=subprocess.PIPE)
>>> a
<subprocess.Popen object at 0x7f8fcef8beb8>
>>> a.stdout.read()
b'/tmp\n'
>>> 

 

a.wait()

a.pid

a.terminate()

a.kill()    # 类似kill -9

import signal

a.kill(1234,signal.SIGTERM)

 

 

 

其他参数  

check=True   若命令有误,直接报错。 不加此参数时,不报错,返回returncode和stderr。

 

posted @ 2018-04-16 21:31  EngineTang  阅读(397)  评论(0编辑  收藏  举报