python基础 -- 执行外部命令

1. os.system(cmd)

>>> import os
>>> os.system('date')
Fri Apr 13 11:00:47 DST 2018 

2. os.popen(cmd)

>>> import os
>>> now = os.popen('date')
>>> now.read() # 读取管道
Fri Apr 13 11:00:47 DST 2018                                                                                 

3. subprocess

>>> import subprocess
>>> now = subprocess.Popen('date', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) # 线程
>>> now.stdout.read()
Fri Apr 13 11:00:47 DST 2018

你可能感兴趣的:(python基础 -- 执行外部命令)