sal
sal

Reputation: 1239

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

I am trying to execute an external command inpython.

The command arguments , if executed in the shell are as follows:

osmconvert inputfile -b=bbox -o=outputfile

I am trying to call it with subprocess as 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])

That gives me the error msg : TypeError: execv() arg 2 must contain only strings

Can anyone hint on how to make this work?

Kind regards!

Upvotes: 0

Views: 5998

Answers (2)

Blckknght
Blckknght

Reputation: 104712

The immediate error you're getting is due to bbox being a tuple of floats, rather than a string. If you want the -b parameter to be passed like -b= 13.400102,52.570951,13.61957,52.676858, you'll probably want to put quotes around the bbox value.

You may have a further issue though. Note the space I put in the parameter string above. If you pass bbox and outputfile as separate parameters from the '-b=' and '-o=' strings, you'll get the equivalent of a space between their values and the equals sign in the command that is called. This may or may not work, depending on how osmconvert handles its command line argument parsing. If you need the -b and -o flags to be part of the same argument as the strings that follow them, I'd suggest using + to concatenate the strings together:

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])

Upvotes: 3

Daniel
Daniel

Reputation: 42758

You need to convert the bbox into the required string representation:

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

Upvotes: 0

Related Questions