gpt4 book ai didi

python - 如何使用子进程Popen?

转载 作者:行者123 更新时间:2023-11-28 18:44:35 25 4
gpt4 key购买 nike

我正在尝试使用 Popen 执行命令。

该命令使用一些 PostGIS/Postgresql 实用程序将栅格文件上传到数据库,并在从命令行执行时起作用。它使用 unix 风格的管道来链接 2 个命令,看起来像这样:

"C:\\Program Files\\PostgreSQL\\9.2\\bin\\raster2pgsql.exe" -d -I -C -e -Y -F -t 128x128 "C:\\temp\\SampleDTM\\SampleDTM.tif" test | "C:\\Program Files\\PostgreSQL\\9.2\\bin\\psql.exe" -h localhost -p 5432 -d adr_hazard -U postgres

在 Python 中使用时,我将其设为带有 ' 代码的字符串:

command = '"C:\\Program Files\\PostgreSQL\\9.2\\bin\\raster2pgsql.exe" -d -I -C -e -Y -F -t 128x128 "C:\\temp\\SampleDTM\\SampleDTM.tif" test | "C:\\Program Files\\PostgreSQL\\9.2\\bin\\psql.exe" -h localhost -p 5432 -d adr_hazard -U postgres'

尝试执行它会导致错误:

p = subprocess.Popen(command)

ERROR: Unable to read raster file: test

错误似乎是命令没有被正确解析(它将错误的参数解释为光栅文件)

我是不是用错了Popen

最佳答案

您的命令 使用管道|。它需要一个外壳:

p = subprocess.Popen(command, shell=True)

据我所知,命令 本身看起来没问题。

关于python - 如何使用子进程Popen?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22090626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com