我想調用一個進程並輸出它的stdd和stout到一個字符串進行檢查。此代碼觸發意外的錯誤塊。subprocess.Popen sterr to string
try:
proc = subprocess.Popen('ls -ddd 1>&2', stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
stdout,stderr = proc.communicate()
if len(stderr)>1:
actualResult =stderr
else:
actualResult =stdout
print actualResult
except:
print "Unexpected error"
我基於它的http://www.oreillynet.com/onlamp/blog/2007/08/pymotw_subprocess_1.html,但我顯然缺少的東西。是否有可能在try塊內完成此操作?