使用os.path的奇怪“UnicodeEncodeError”存在`

2024-04-28 03:15:05 发布

您现在位置:Python中文网/ 问答频道 /正文

在web应用程序(使用Flask)中,我得到以下错误:

Unable to retrieve the thumbnail for u'/var/data/uploads/2012/03/22/12 Gerd\xb4s Banjo Trio 1024.jpg'
Traceback (most recent call last):
 File "/var/www/beta/env/lib/python2.7/site-packages/dblib-1.0dev3-py2.7.egg/dblib/orm/file.py", line 169, in get_thumbnail
   if not exists(filename):
 File "/usr/lib/python2.7/genericpath.py", line 18, in exists
   os.stat(path)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xb4' in position 52: ordinal not in range(128)

注意,我在记录的错误中包含了文件名的repr()。这表明文件名是作为Unicode实例传递的。这么多是正确的。。。

如果我使用python解释器运行罪犯,它将按预期工作:

>>> from os.path import exists
>>> exists(u'/var/data/uploads/2012/03/22/12 Gerd\xb4s Banjo Trio 1024.jpg')
True

所以很明显,在Flask环境中运行时,Python认为应该使用ASCII编解码器而不是UTF-8编码文件名。我在Apache httpd后面使用mod_wsgi部署了应用程序。

我想我必须告诉他们中的任何一个在某处使用UTF-8?但是在哪里?


Tags: in应用程序flaskdatatrio文件名var错误