Python子进程不传递参数

2024-06-03 03:33:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用osmosis自动执行从大型OSM文件中提取数据的过程,但在运行此代码段以自动从OSM数据创建平铺时遇到问题:

import sys
import subprocess

def create_tile_pbf(pbf_file, tile_lng, tile_lat):
    """Runs the osmosis tile creator"""
    app_path = "osmosis/bin/"
    app = "osmosis.bat"
    proc = subprocess.Popen([
        app_path + app,
        "--read-pbf", pbf_file,
        "--bounding-box",
        "top=%d" % (tile_lat+1),
        "left=%d" % (tile_lng),
        "bottom=%d" % (tile_lat),
        "right=%d" % (tile_lng+1),
        "--write-pbf", "someotherfile.osm.pbf"
        ], cwd=app_path, shell=True)
    # Poll the proc to see if it is finished
    while proc.returncode is None:
        proc.communicate()
        proc.poll()
    print(">> Terminated\n")

if __name__ == "__main__":
    print("Args were: " + str(sys.argv) + "\n")
    create_tile_pbf("./somefile.osm.pbf", 7, 46)

我尝试使用和不使用shell=True,我尝试将所有参数合并到一个字符串中。但我在执行时总是会遇到这样的错误:

^{pr2}$

但是当使用Powershell或命令提示符运行命令时,它的工作方式就像一个符咒。在

.\osmosis.bat --read-pbf .\somefile.osm.pbf --bounding-box top=47 left=7 bottom=46 right=8 --write-pbf someotherfile.osm.pbf

我正在运行Windows 10 1703 64位Anaconda 4.3.30。在


Tags: 数据pathimportapposmcreatesysproc
1条回答
网友
1楼 · 发布于 2024-06-03 03:33:27

脚本和命令提示符交互之间的区别是osmosis/bin/osmosis.bat.\osmosis.bat。由于对Popen()的调用已经包含了cwd选项,因此不需要再次指定目录。这意味着你的电话应该是:

proc = subprocess.Popen([
    app,                                    # <== No app_path here
    " read-pbf", pbf_file,
    " bounding-box",
    "top=%d" % (tile_lat+1),
    "left=%d" % (tile_lng),
    "bottom=%d" % (tile_lat),
    "right=%d" % (tile_lng+1),
    " write-pbf", "someotherfile.osm.pbf"
    ], cwd=app_path, shell=True)           # <== because of this cwd 

相关问题 更多 >