Bil*_* VB 0 linux postgresql subprocess
使用命令行,我确认以下命令正确执行
echo '\c mydatabase;\i db-reset.sql' | psql -U postgres -h localhost
Run Code Online (Sandbox Code Playgroud)
但是,在Python中,我可以确认以下几行绝对没有,并返回状态代码0.
import subprocess
code = subprocess.call(r"echo '\c mydatabase;\i db-reset.sql' | psql -U postgres -h localhost", shell=True)
assert code == 0 # This comes to true
Run Code Online (Sandbox Code Playgroud)
从本质上讲,为什么使用子进程调用的命令实际上没有做任何事情?
它可以工作,但你需要更多的反斜杠.另外,我建议你不要shell=True在这里使用.
这就是你做的,但没有shell:
p = subprocess.Popen(['psql', '-U', 'postgres', '-h', 'localhost'], shell=False, stdin=subprocess.PIPE)
p.communicate(r"\c mydatabase;\i db-reset.sql")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1341 次 |
| 最近记录: |