我的代码是
file_size = os.path.getsize(file_path)
当 file_path 中存在 Unicode 字符时出现以下异常
File "C:\Python27\lib\genericpath.py", line 57, in getsize
return os.stat(filename).st_size
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'E:\\Backup\\MyFolder\\~$??????.docx'
注意:当我通过 Python 3 运行相同的代码时,我没有收到相同的错误。
我的问题是如何修复代码以在 Python 2 和 3 上运行?