【发布时间】:2018-12-06 17:53:06
【问题描述】:
我想在我的 Python 代码中使用“raster2pgsql”实用程序。当我在 Linux 终端中使用它时,它工作正常。这是命令:
$ raster2pgsql -a "/mnt/c/Users/Jan/path/to/raster/dem.tiff" test_schema.raster2 | psql -h localhost -d pisl -U pisl
然后我使用 subprocess.run(我也尝试过 subprocess.call)在我的 Python 代码中使用相同的工具。这是我的代码:
from subprocess import run
command = ["raster2pgsql", "-a", '"' + file_name + '"', self.schema_name + "." + identifier, "|", "psql", "-h", "localhost", "-p", "5432", "-d", self.dbname]
run(command)
我收到此错误:
ERROR: Unable to read raster file: "/mnt/c/Users/Jan/path/to/raster/dem.tiff"
打印command 给出了我认为是正确的(相当于终端中的工作):
['raster2pgsql', '-a', '"/mnt/c/Users/Jan/path/to/raster/dem.tiff"', 'test_schema.raster2', '|', 'psql', '-h', 'localhost', '-p', '5432', '-d', 'pisl']
我已经仔细检查了光栅文件的路径是否正确,尝试了单引号、双引号但没有任何帮助。我查看了许多类似的问题(here、here 或 here),但没有发现任何有用的信息。
我在 Windows 10 中使用 Python 3.5 和 Linux Bash Shell。
问题:我使用子流程的方式有什么问题?
【问题讨论】:
-
你为什么要引用文件名?
-
因为当我不识别文件名时它无法识别。当我删除引号时,它认为“test_schema.raster2”是文件名。
-
我提供了另一种方法,将 2 个命令连接在一起。请测试一下。如果有错误,请准确报告。我怀疑引号是问题所在,因为 shell 在您执行的工作命令中删除了它们。
标签: python python-3.x terminal subprocess