Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python subprocess and unicode execv() arg 2 must contain only strings

I have a django site where I need to call a script using subprocess. The subprocess call works when I'm using ascii characters but when I try to issue arguments that are utf-8 encoded, I get an error:

execv() arg 2 must contain only strings.

The string u'Wiadomo\u015b\u0107' is coming from a postgres db. This example is using polish words. When I run it using english words, I have no issues.

The call looks like this:

subprocess.Popen(['/usr/lib/p3web2/src/post_n_campaigns.py', '-c', u'bm01', '-1', u'Twoja', '-2', u'Wiadomo\u015b\u0107', '-3', u'', '-4', u'', '-5', u'', '-6', u'', '-m', u'pl', '-p', 'yes'])

I'm not sure how to handle the strings in this case. The odd thing is that this works fine when i run it through the python interpreter.

like image 316
deecodameeko Avatar asked Sep 30 '11 15:09

deecodameeko


1 Answers

You should encode the Unicode strings in the encoding your program expects. If you know the program expects UTF-8:

u'Wiadomo\u015b\u0107'.encode('utf8')

If you don't know what encoding you need, you could try your platform's default encoding:

u'Wiadomo\u015b\u0107'.encode()
like image 54
Mark Byers Avatar answered Sep 21 '22 17:09

Mark Byers