特殊字符作为我的python脚本的一部分,我测试,看看这两个文件使用相同的尺寸:Python的 - “系统找不到指定文件”由于文件名
os.path.getsize(dir_file) # dir_file = root path + filename joined
但当我遇到一个名称中包含特殊字符的文件时(例如Ü),我得到以下错误:WindowsError: [Error 2] The system cannot find the file specified
替换为特殊字符\xf6
。
我已经试过编码dir_file
为UTF-8,像这样:
unicode(dir_file, 'utf-8') # method 1
dir_file.encode('utf-8') # method 2
但是,这使我有以下错误:UnicodeDecodeError: 'ascii' codec can't decode byte 0xf6 in position 79: ordinal not in range(128)
。
不知道如何解决这个字符编码问题。
我们可以猜到,但是您应该在标签中指定您的Python版本。 Python 2和Python 3处理Unicode的方式非常不同。 –
你是如何初始化和准确加入变量dir_file的内容的? – anneb
@RoryDaulton哦,好的。我使用Python 2 – sookie