Take this example snippet.
import subprocess
import os
env = os.environ.copy()
env["FOO"] = u"foo"
subprocess.check_call(["ls", "-l"], env=env)
On Windows, this fails.
C:\Python27\python.exe test.py
Traceback (most recent call last):
File "test.py", line 7, in <module>
subprocess.check_call(["ls", "-l"], env=env)
File "C:\Python27\lib\subprocess.py", line 535, in check_call
retcode = call(*popenargs, **kwargs)
File "C:\Python27\lib\subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "C:\Python27\lib\subprocess.py", line 710, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 958, in _execute_child
startupinfo)
TypeError: environment can only contain strings
sys.path
is documented to be perfectly ok with unicode. What is the correct way to deal with this (and similar code) so that everything works as expected? The obvious solution is to call .encode()
on the unicode path but I'm not sure if that will lead to unexpected behaviours.
Aucun commentaire:
Enregistrer un commentaire