Unicode filenames on Windows with Python & subprocess.Popen()

In Py3K – at least from Python 3.2 – subprocess.Popen and sys.argv work consistently with (default unicode) strings on Windows. CreateProcessW and GetCommandLineW are used obviously.

In Python – up to v2.7.2 at least – subprocess.Popen is buggy with Unicode arguments. It sticks to CreateProcessA (while os.* are consistent with Unicode). And shlex.split creates additional nonsense.

Pywin32’s win32process.CreateProcess also doesn’t auto-switch to the W version, nor is there a win32process.CreateProcessW. Same with GetCommandLine.
Thus ctypes.windll.kernel32.CreateProcessW... needs to be used.
The subprocess module perhaps should be fixed regarding this issue.

UTF8 on argv[1:] with private apps remains clumsy on a Unicode OS. Such tricks may be legal for 8-bit “Latin1” string OSes like Linux.

UPDATE vaab has created a patched version of Popen for Python 2.7 which fixes the issue.
See https://gist.github.com/vaab/2ad7051fc193167f15f85ef573e54eb9
Blog post with explanations: http://vaab.blog.kal.fr/2017/03/16/fixing-windows-python-2-7-unicode-issue-with-subprocesss-popen/

Leave a Comment