python - UnicodeDecodeError - Error while reading the file

I am getting a UnicodeDecodeError when reading a file that has non-ascii characters. Here is the snippet of code

import codecs

import locale

print locale.getpreferredencoding()

fname = "c:\\testing\nonascii.txt"

f=codecs.open(fname,"r",encoding='utf-8')

sfile=f.read()

print type(file) #it's unicode

print sfile.encode('utf-8')

print type(sfile.encode('utf-8'))

网友答案:

Judging by the filename, you're using Windows. Files on Windows will not be UTF-8 encoded unless you take special care to save them that way; by default they will use your code page.

If you don't know what code page Windows is using, you can use the special encoding mbcs to get what it uses for a default. If you want your program to work on other systems besides Windows, you can use sys.getfilesystemencoding() to get a value that should work on the current system; on Windows it will return mbcs.