I get this error:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 4: ordinal not in range(128)
I tried setting many different codecs (in the header, like # -- coding: utf8 --), or even using u"string", but it still appears.
How do I fix this?
Edit: I don't know the actual character that's causing this, but since this is a program that recursively browses folders, it must have found a file with strange characters in its name
Code:
# -*- coding: utf8 -*-
# by TerabyteST
###########################
# Explores given path recursively
# and finds file which size is bigger than the set treshold
import sys
import os
class Explore():
def __init__(self):
self._filelist = []
def exploreRec(self, folder, treshold):
print folder
generator = os.walk(folder + "/")
try:
content = generator.next()
except:
return
folders = content[1]
files = content[2]
for n in folders:
if "$" in n:
folders.remove(n)
for f in folders:
self.exploreRec(u"%s/%s"%(folder, f), treshold)
for f in files:
try:
rawsize = os.path.getsize(u"%s/%s"%(folder, f))
except:
print "Error reading file %s"%u"%s/%s"%(folder, f)
continue
mbsize = rawsize / (1024 * 1024.0)
if mbsize >= treshold:
print "File %s is %d MBs!"%(u"%s/%s"%(folder, f), mbsize)
Error:
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
a.exploreRec("C:", 100)
File "D:/Python/Explorator/shitfinder.py", line 35, in exploreRec
print "Error reading file %s"%u"%s/%s"%(folder, f)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe0 in position 4: ordinal not in range(128)
Here is what is shown using print repr("Error reading file %s"%u"%s/%s"%(folder.decode('utf-8','ignore'), f.decode('utf-8','ignore')))
>>> a = Explore()
>>> a.exploreRec("C:", 100)
File C:/Program Files/Ableton/Live 8.0.4/Resources/DefaultPackages/Live8Library_v8.2.alp is 258 MBs!
File C:/Program Files/Adobe/Reader 9.0/Setup Files/{AC76BA86-7AD7-1040-7B44-A90000000001}/Data1.cab is 114 MBs!
File C:/Program Files/Microsoft Games/Age of Empires III/art/Art1.bar is 393 MBs!
File C:/Program Files/Microsoft Games/Age of Empires III/art/art2.bar is 396 MBs!
File C:/Program Files/Microsoft Games/Age of Empires III/art/art3.bar is 228 MBs!
File C:/Program Files/Microsoft Games/Age of Empires III/Sound/Sound.bar is 273 MBs!
File C:/ProgramData/Microsoft/Search/Data/Applications/Windows/Windows.edb is 162 MBs!
REPR:
u"Error reading file C:/ProgramData/Microsoft/Windows/GameExplorer/{1B4801C1-CA86-487E-8347-B26F1CCB2F75}/SupportTasks/0/Sito web di Mirror's Edge.lnk"
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/GameExplorer/{1B4801C1-CA86-487E-8347-B26F1CCB2F75}/SupportTasks/0/Sito web di Mirror's Edge.lnk
REPR:
u"Error reading file C:/ProgramData/Microsoft/Windows/GameExplorer/{1B4801C1-CA86-487E-8347-B26F1CCB2F75}/SupportTasks/1/Contenuti scaricabili di Mirror's Edge.lnk"
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/GameExplorer/{1B4801C1-CA86-487E-8347-B26F1CCB2F75}/SupportTasks/1/Contenuti scaricabili di Mirror's Edge.lnk
REPR:
u'Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Google Talk/Supporto/Modalitiagnostica di Google Talk.lnk'
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Google Talk/Supporto/Modalitiagnostica di Google Talk.lnk
REPR:
u'Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Microsoft SQL Server 2008/Strumenti di configurazione/Segnalazione errori e utilizzo funzionaliti SQL Server.lnk'
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Microsoft SQL Server 2008/Strumenti di configurazione/Segnalazione errori e utilizzo funzionaliti SQL Server.lnk
REPR:
u'Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Mozilla Firefox/Mozilla Firefox ( Modalitrovvisoria).lnk'
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Mozilla Firefox/Mozilla Firefox ( Modalitrovvisoria).lnk
REPR:
u'Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Mozilla Firefox 3.6 Beta 1/Mozilla Firefox 3.6 Beta 1 ( Modalitrovvisoria).lnk'
END REPR:
Error reading file C:/ProgramData/Microsoft/Windows/Start Menu/Programs/Mozilla Firefox 3.6 Beta 1/Mozilla Firefox 3.6 Beta 1 ( Modalitrovvisoria).lnk
Traceback (most recent call last):
File "<pyshell#21>", line 1, in <module>
a.exploreRec("C:", 100)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
File "D:/Python/Explorator/shitfinder.py", line 30, in exploreRec
self.exploreRec(("%s/%s"%(folder, f)).encode("utf-8"), treshold)
UnicodeDecodeError: 'ascii' codec can't decode byte 0x99 in position 78: ordinal not in range(128)
>>>