python 004 执行环境对比
阅读原文时间:2023年07月16日阅读:1

对比:os.system os.popen subprocess.Popen subprocess.call

为什么要搞这么多?

# --*--encoding: utf-8--*--
import os
import subprocess

os_system = os.system('dir')
print os_system
#只获取了返回值,如果获取输出需如下:
re = os.system('dir>C:\Users\Administrator\PycharmProjects\dir.txt')
print re
print open('C:\Users\Administrator\PycharmProjects\dir.txt','r').readlines()

re1 = os.popen('dir')
print re1.read()
print '-' * 20

re2 = subprocess.Popen('dir', shell=True, stdout=subprocess.PIPE)
print re2
print re2.stdout.read()

re3 = subprocess.call(('dir','C:/Users/Administrator/PycharmProjects/'),shell=True)
print re3