12

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.

rypel
  • 4,686
  • 2
  • 25
  • 36
deecodameeko
  • 505
  • 7
  • 18

1 Answers1

17

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()
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452