Python 子进程不传递参数
Python subprocess does not transfer arguments
我想使用 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
,我尝试将所有参数连接到一个字符串中。但是我执行的时候总是报这个错:
Nov 03, 2017 2:26:28 PM org.openstreetmap.osmosis.core.Osmosis main
SEVERE: Execution aborted.
org.openstreetmap.osmosis.core.OsmosisRuntimeException: Expected argument 1 to be an option or task name.
at org.openstreetmap.osmosis.core.cli.CommandLineParser.parse(CommandLineParser.java:79)
at org.openstreetmap.osmosis.core.Osmosis.run(Osmosis.java:74)
at org.openstreetmap.osmosis.core.Osmosis.main(Osmosis.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:330)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:238)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
但是当 运行 使用 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。
您的脚本和命令提示符交互之间的区别是 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
我想使用 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
,我尝试将所有参数连接到一个字符串中。但是我执行的时候总是报这个错:
Nov 03, 2017 2:26:28 PM org.openstreetmap.osmosis.core.Osmosis main
SEVERE: Execution aborted.
org.openstreetmap.osmosis.core.OsmosisRuntimeException: Expected argument 1 to be an option or task name.
at org.openstreetmap.osmosis.core.cli.CommandLineParser.parse(CommandLineParser.java:79)
at org.openstreetmap.osmosis.core.Osmosis.run(Osmosis.java:74)
at org.openstreetmap.osmosis.core.Osmosis.main(Osmosis.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchStandard(Launcher.java:330)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:238)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at org.codehaus.classworlds.Launcher.main(Launcher.java:47)
但是当 运行 使用 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。
您的脚本和命令提示符交互之间的区别是 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