0

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!

3
  • Try str(inputfile) ? You could also do the same for the other. (output file) Commented Oct 17, 2015 at 18:11
  • What part of arg 2 must contain only strings is unclear? Commented Oct 17, 2015 at 18:13
  • tried inputfile = str('/path/to/inputfile' ) outputfile = str('/path/to/outputfile') , same error :-( Commented Oct 17, 2015 at 18:15

2 Answers 2

3

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])
Sign up to request clarification or add additional context in comments.

6 Comments

also, ['osmconvert', inputfile, '-b', bbox, '-o', outputfile] might work (no =, separate arguments).
@J.F.Sebastian: That might work, I'm not sure. Like I said, it depends on how osmconvert parses its parameters. All the example usages I see online use the equals sign and concatenated values, but the tool itself might be more forgiving than the examples show.
Ok the concatenation using the + made it work finally ! Thanks !! And @J.F.Sebastian : In the case of osmconvert I cant leave away the =, I tried, didnt work.
@ray: did you drop = and put the values into separate arguments (no +)?
I used it like that test = sb.Popen(['osmconvert', inputfile, '-b='+bbox, '-o='+outputfile])
|
0

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

1 Comment

I doubt %d is the right conversion to use for floating point values, since it will truncate them to integers. Probably %f would be more appropriate, though you might want to specify a precision.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.