Using Python 2.6.5 on Windows XP, I'm processing a directory of files by calling os.stat on them to obtain their size. The script is failing when it reaches a particular file that happens to have non-ASCII characters embedded within the name. The exception being thrown is that os.stat couldn't find the file specified. I know the file is there because I can play in iTunes or VLC Media Player.
The name of the file in question is
1-02 Só Danço Samba (Jazz Samba).m4a
Just in case the characters aren't being displayed, the string is
'1-02 So\xb4 Danc\xb8o 开发者_运维百科Samba (Jazz Samba).m4a'
Is there something I should or could be doing to make the name acceptable to os.stat? BTW, trying to open the file in python also fails for the same reason.
Try inserting the line # coding=UTF-8
at the top of your python file (only makes a difference for unicode in your script, as Philipp points out), and make sure you are storing the file names as unicode instead of str.
Tested with the following:
# coding=UTF-8
import os
fname = u'/temp/1-02 Só Danço Samba (Jazz Samba).m4a'
print(os.stat(fname))
精彩评论