Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在编写一个将文件路径保存到数据库的小型应用程序(使用 django)。我假设文件路径是 utf-8 编码的,但我遇到了以下文件名:C:\FXG™.nfo这显然不是用 utf-8 编码的。
C:\FXG™.nfo
当我这样做时,filepath.decode('utf-8')我收到以下错误:
filepath.decode('utf-8')
UnicodeDecodeError:“utf8”编解码器无法解码0x99位置 30 中的字节:无效的起始字节
UnicodeDecodeError:
0x99
(我修剪了文件名,所以这里的位置不对)。
我如何知道文件路径是如何以适用于每个文件名的方式编码的?
使用sys.getfilesystemencoding().
sys.getfilesystemencoding()
这应该允许您转换所有看起来不错的路径。
但是,总有可能存在非法编码的文件或文件夹,您必须考虑如何在您的应用程序框架中处理这些文件或文件夹。
某些应用程序可能会忽略此类文件,而其他应用程序则将名称保留为二进制 blob。