python 004 執行環境對比
阿新 • • 發佈:2017-06-20
為什麽 返回值 logs code ini dmi spa 執行 python 對比: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
python 004 執行環境對比