Skip to content

Instantly share code, notes, and snippets.

@taojy123
Created October 25, 2018 03:14
Show Gist options
  • Save taojy123/6ffeea3eeef53bce5df22fa3c97203db to your computer and use it in GitHub Desktop.
Save taojy123/6ffeea3eeef53bce5df22fa3c97203db to your computer and use it in GitHub Desktop.
python 执行命令行 获取结果
# ============ python2 ====================
import commands
commands.getoutput("ls")
commands.getstatus("ls")  
status, output = commands.getstatusoutput("ls")  
# ============ python3 ====================
from subprocess import Popen, PIPE
p = Popen("ls", shell=True, stdout=PIPE, stderr=PIPE)
p.wait()
p.stdout.read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment