0

I have a unicode string. When I print it it, copy and paste it into the CMD and press Enter - it's all working well. But When I call the function subprocess.Popen() with that command string it doesn't work, Python says there is no such file in the path and prints a path in gibberish (the unicode part in the original string is replaced with weird looking characters).

Assume I have the string (actually a type<unicode>) called s (by the way, my string contains a command running a python script from an external folder, so it looks something like this - "python 'C:/path/to/file/file.py' arg1 arg2'), I can do:

print s #this works, even just copying and pasting it manually into the command line works fantastically
subprocess.Popen(s, stdout=subprocess.PIPE) #prints 'python: can't open file [gibberish path]: Errno 2 no such file or directory' 

And the reason why I don't use any other function aside of Popen(), say system() or call() is because I need the output of the program.

Any idea of what I can do?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Jonathan
  • 123
  • 2
  • 7
  • 1
    Perhaps there is a problem with interpreting the string as utf-8 - if python thinks it is another encoding than utf-8 it might encode it wrongly, is this python3? – gauteh May 24 '16 at 14:32
  • 1
    If the string looks wrong when printed, how do you know that it's correct? – dorian May 24 '16 at 14:33
  • @gauteh Nope, 2.7.11 – Jonathan May 24 '16 at 14:37
  • @dorian It looks okay when printing, only wrong when python prints it. I rephrased the question now – Jonathan May 24 '16 at 14:38
  • It might be a problem with the shell that is started by subprocess; if that messes up the unicode argument it gets, Python can't do anything with it. Typically, the shell started by subprocess is more bare bones than your usual shell. –  May 24 '16 at 14:39
  • 1
    Is the type of `script_name` `unicode` or `str`? If the latter, what encoding is it in? Is it the same as the encoding returned by [`sys.getfilesystemencoding()`](https://docs.python.org/2.7/library/sys.html#sys.getfilesystemencoding)? – jwodder May 24 '16 at 14:40
  • Is [this question](http://stackoverflow.com/questions/22475791/unicode-and-subprocess-popen) on subprocess and unicode of any use? –  May 24 '16 at 14:40
  • @Jonathan Thanks for the clarification. I agree with gauteh et al. that it's likely an encoding issue. – dorian May 24 '16 at 14:41
  • @jwodder It's unicode, utf-8. And it's not the same, the encoding returned by the function you stated is mbcs. – Jonathan May 24 '16 at 14:49
  • @Jonathan: If the variable is of type `unicode`, it can't be UTF-8, as that only applies to characters encoded as byte sequences (i.e., `str` objects). Is there a `script_name.encode('utf-8')` expression you're not showing us? – jwodder May 24 '16 at 14:52
  • @jwodder Nope, I was just confused. It is of type unicode, though. – Jonathan May 24 '16 at 15:03
  • I rephrased the whole question, might be worth to look at the changes, maybe it's even more clarified now – Jonathan May 24 '16 at 16:53
  • In Python2, convert unicode string to bytes using `sys.getfilesystemencoding()` (which may be occasionally be `None` or wrong, so please test a few systems) – Dima Tisnek May 24 '16 at 17:07
  • @qarma It actually did work! Thank you – Jonathan May 24 '16 at 18:01
  • related: https://bugs.python.org/issue1759845 – jfs May 26 '16 at 13:54

0 Answers0