Python运行子进程,参数用双引号括起来,子目录中有空格

2024-04-25 00:23:49 发布

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

我想运行一个可执行程序,它位于我的脚本路径下的目录中。路径包含空格,程序参数需要双引号。在

这似乎是这里已经提出的其他问题的组合(Handling directories with spaces Python subprocess.call()Passing double quote shell commands in python to subprocess.Popen()?),但没有一个能为我的问题提供一个合适的解决方案。在

这是我的代码:

import subprocess

subdir = 'ogr\\'
command = subdir + 'ogr2ogr.exe -f "ESRI Shapefile" output0.shp input0.kml'

output,error = subprocess.Popen(
    command, universal_newlines=True, shell=True, 
    stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()

我试图运行的可执行文件(ogr2ogr.exe文件)驻留o 在我的脚本下面的“ogr\”子目录中,该子目录又位于包含空格的目录中(例如,C:\my Documents\my Apps\Scripts\myscript.py). 其中一个参数需要双引号。如果我将脚本和输入文件(input0.kml)放在可执行文件的同一子目录中,上述命令可以处理参数中的双引号,但如果我试图在脚本本身的同一目录中运行,则会出现“系统找不到指定文件”的消息。在

有什么提示吗?在

提前谢谢!在


Tags: 文件路径目录脚本参数shellexecommand