TypeError:execv()arg 2必须只包含subprocess.Popen上的字符串

时间:2015-10-17 18:08:53

标签: python arguments subprocess parameter-passing popen

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

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

osmconvert inputfile -b=bbox -o=outputfile

我试图用subprocess作为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

任何人都可以暗示如何使这项工作?

亲切的问候!

2 个答案:

答案 0 :(得分:3)

您获得的即时错误是由于bbox是浮点元组而不是字符串。如果您希望像-b那样传递-b= 13.400102,52.570951,13.61957,52.676858参数,那么您可能希望在bbox值附近加上引号。

你可能还有一个问题。请注意我在上面的参数字符串中放置的空间。如果您将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])

答案 1 :(得分:0)

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

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