Files
python3-cookbook/source/c05/p14_bypassing_filename_encoding.rst
2015-12-28 19:34:04 +08:00

60 lines
1.8 KiB
ReStructuredText
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

==============================
5.14 忽略文件名编码
==============================
----------
问题
----------
你想使用原始文件名执行文件的I/O操作也就是说文件名并没有经过系统默认编码去解码或编码过。
----------
解决方案
----------
默认情况下,所有的文件名都会根据 ``sys.getfilesystemencoding()`` 返回的文本编码来编码或解码。比如:
.. code-block:: python
>>> sys.getfilesystemencoding()
'utf-8'
>>>
如果因为某种原因你想忽略这种编码,可以使用一个原始字节字符串来指定一个文件名即可。比如:
.. code-block:: python
>>> # Wrte a file using a unicode filename
>>> with open('jalape\xf1o.txt', 'w') as f:
... f.write('Spicy!')
...
6
>>> # Directory listing (decoded)
>>> import os
>>> os.listdir('.')
['jalapeño.txt']
>>> # Directory listing (raw)
>>> os.listdir(b'.') # Note: byte string
[b'jalapen\xcc\x83o.txt']
>>> # Open file with raw filename
>>> with open(b'jalapen\xcc\x83o.txt') as f:
... print(f.read())
...
Spicy!
>>>
正如你所见,在最后两个操作中,当你给文件相关函数如 ``open()````os.listdir()``
传递字节字符串时,文件名的处理方式会稍有不同。
----------
讨论
----------
通常来讲,你不需要担心文件名的编码和解码,普通的文件名操作应该就没问题了。
但是,有些操作系统允许用户通过偶然或恶意方式去创建名字不符合默认编码的文件。
这些文件名可能会神秘地中断那些需要处理大量文件的Python程序。
读取目录并通过原始未解码方式处理文件名可以有效的避免这样的问题,
尽管这样会带来一定的编程难度。
关于打印不可解码的文件名请参考5.15小节。