我想的Python的解析子輸出
git remote show origin
輸出保存
tempf = open('.tmp', 'w+')
tempf2 = open('.tmp2', 'w+')
subprocess.Popen(["git", "remote", "show", "origin"], stdout=tempf, stderr=tempf2)
tempf.close()
tempf2.close()
output = open('.tmp', 'r')
gitoutput = output.read()
後來解析與正則表達式的輸出。
但是,上面的代碼一直返回None
爲gitoutput
。
有什麼,我失蹤?我很困惑,因爲申請.seek(0)
不會改變輸出並且運行cat .tmp
顯示正確的內容。
編輯:stderr
也被捕獲(stderr=tempf2
),並在運行腳本時git服務器產生不需要的輸出到命令行。
你可以試試'subprocess.check_call'而不是 – 2014-11-05 00:16:18