我使用調用方法加載SQL轉儲:蟒子(或相關) - 負載PSQL突降,遇到錯誤 - 「<」忽略
call(["psql", "-U", "user", "name", "<", "pathtofile"])
這直接登錄到Postgres的,而忽略「<」和pathtofile。
psql: warning: extra command-line argument "<" ignored
我使用調用方法加載SQL轉儲:蟒子(或相關) - 負載PSQL突降,遇到錯誤 - 「<」忽略
call(["psql", "-U", "user", "name", "<", "pathtofile"])
這直接登錄到Postgres的,而忽略「<」和pathtofile。
psql: warning: extra command-line argument "<" ignored
使用stdin
關鍵字參數,如果你想重定向:
with open("pathtofile", "rb") as f:
call(["psql", "-U", "user", "name"], stdin=f)
嘗試改變這樣的:
call(["psql", "-U", "user", "name", "<", "pathtofile"], shell=True)
^^^^^^^^^^^^
「<」 是一個輸入重定向操作,必須由一個解釋貝殼。如果您沒有說shell=True
,它會作爲參數傳遞給psql
,而psql
不知道如何處理它。
編輯:一般來說,@falsetru建議的是更好的方法,因爲將任意字符串傳遞給shell可能很危險。但如果你需要需要來使用shell操作,shell=True
就是這樣做的方法。
[接受答案](http://meta.stackexchange.com/a/5235)如果你得到工作答案。 – falsetru