编码的Unicode路径打破它
我有在内存中的以下路径:我想用它作为CMD参数编码的Unicode路径打破它
video_path = u'C:\\Documents and Settings\\user\\My Documents\\Downloads\\\xf5iv - Neon Phoenix [Free DL].mp3'
,所以我必须要对其进行编码。
video_path = video_path.encode(sys.getfilesystemencoding())
cmd = 'ffmpeg -y -i "%s" -vn -ac 2 -f mp3 audio.mp3' % video_path
subprocess.Popen(cmd)
但字符串是不正确的方式编码 - 它转换\xf5
到?
而不是õ
。因此无法找到该文件。
这是怎么发生的?我正在使用默认的文件系统编码(即mbcs)。
从一个答案here:
在Py3K - 从 “巨蟒” 至少3.2 - subprocess.Popen和sys.argv中 工作始终与(缺省Unicode)在Windows海峡的。 明显使用CreateProcessW和GetCommandLineW。
在Python - 至多v2.7.2至少 - subprocess.Popen与 unicode参数的越野车。它坚持CreateProcessA(而操作系统*是 与unicode一致)。 shlex.split创建了额外的废话。 Pywin32的win32process.CreateProcess也不会自动切换到W 版本,也没有win32process.CreateProcessW。与 GetCommandLine相同。因此ctypes.windll.kernel32.CreateProcessW ...需要使用 。子进程模块或许应该被固定为关于这个 问题。
因此,subprocess.Popen
不能处理在Python的2.X版本的Unicode权。
我的解决方案是将输入文件重命名为随机数(使用os.rename
,它支持Unicode),使用ffmpeg
进行转换,我使用subprocess.Popen
启动,然后重新命名。
尝试使用UTF-8编码:
video_path = video_path.encode("utf-8")
Windows不在其文件系统中使用UTF-8。 – 2012-04-23 21:54:21
除非我完全错了,在
video_path = u'C:...\\xf5iv...'
双反斜线导致问题。应该只有一个:
video_path = u'C:...\xf5iv...'
否则反斜杠被保存为一个反斜杠和离开os.system()
,而不是.encode()
,来应对。
Windows使用Unicode路径。你为什么编码你的Unicode字符串? – 2012-04-23 21:51:58
@André:它使用“Unicode”,而不是Unicode。 – 2012-04-23 21:54:53
现在我在猜测,但如果你将'video_path'作为一个unicode对象(不编码它),将'cmd = u'..'%videopath'作为一个Unicode来构造,然后在末尾进行编码会发生什么? '使用os.system(cmd.encode(sys.getfilesystemencoding()))'?在Linux和Python 2.7上它没有区别,但它可能值得您在平台上尝试。 – jogojapan 2012-04-25 06:14:05