TypeError: execv() arg 2 must contain only strings on subprocess.Popen

TypeError: execv() arg 2 must contain only strings on subprocess.Popen

我正在尝试在 python 中执行外部命令。

如果在 shell 中执行,命令参数如下:

osmconvert inputfile -b=bbox -o=outputfile

我正在尝试用子进程调用它作为 fowlloows:

import subprocess as sb

inputfile = '/path/to/inputfile'    
outputfile = '/path/to/outputfile'
bbox = 13.400102,52.570951,13.61957,52.676858

test = sb.Popen(['osmconvert', inputfile, '-b=', bbox, '-o=',outputfile])

这给了我错误消息:TypeError: execv() arg 2 must contain only strings

任何人都可以提示如何进行这项工作吗?

亲切的问候!

您需要将 bbox 转换为所需的字符串表示形式:

test = sb.Popen(['osmconvert', inputfile, '-b', '%d,%d,%d,%d' % tuple(bbox), '-o',outputfile])

你得到的直接错误是由于 bbox 是浮点数的元组,而不是字符串。如果您希望 -b 参数像 -b= 13.400102,52.570951,13.61957,52.676858 一样传递,您可能需要在 bbox 值周围加上引号。

您可能还有其他问题。请注意我在上面的参数字符串中输入的 space 。如果您将 bboxoutputfile 作为单独的参数从 '-b=''-o=' 字符串传递,您将在它们的值和在调用的命令中输入等号。这可能有效也可能无效,具体取决于 osmconvert 如何处理其命令行参数解析。如果您需要 -b-o 标志与它们后面的字符串属于同一参数,我建议使用 + 将字符串连接在一起:

inputfile = '/path/to/inputfile'    
outputfile = '/path/to/outputfile'
bbox = '13.400102,52.570951,13.61957,52.676858' # add quotes here!

 # concatenate some of the args with +
test = sb.Popen(['osmconvert', inputfile, '-b='+bbox, '-o='+outputfile])