我们中的许多人可能都会遇到这个问题,但我在unicode处理方面做得很差。问题是:这是一个代码片段,我试图执行.exe文件,并检查文件路径是否存在,但没有成功:
#Python 2.6.7
filePath = 'C:\\Test\\' # Test folder haveing file BitComet_比特彗星_1_25.exe
for (adir, dirs, files) in os.walk(rootdir):
for f in files:
path = os.path.join(adir,f)
if os.path.exists(path ):
print'Path Found',path
#Extract file
#logging(path )
else:
print 'Path Not Found'
#logging(path )我总是得到“找不到路径”的结果。我尝试使用path.decode('utf-8'):
但该脚本将文件路径读取为:
C:\Test\BitComet_????_1_25.exe 由于此文件路径不存在,因此它转到else分支。
请给我一个提示来处理这个unicode的问题,如果我能够向用户显示cmd上的文件路径或在日志文件中显示文件路径是否更好。
如果这似乎是一个重复的帖子,我道歉。
发布于 2013-04-05 20:09:09
Windows路径采用UTF-16编码。Python可以为您处理此问题,只需将unicode路径传递给os.walk(),您将获得Unicode结果:
filePath = u'C:\\Test\\' # Test folder haveing file BitComet_比特彗星_1_25.exe
for (adir, dirs, files) in os.walk(filePath):https://stackoverflow.com/questions/15833509
复制相似问题