我试图按以下方式打开文件:
Traceback (most recent call last):
File "C:\Users\Manoj\AppData\Local\Programs\Python\Python36\lib\site-packages\
tensorflow\python\pywrap_tensorflow.py", line 41, in <module>
from tensorflow.python.pywrap_tensorflow_internal import *
File "C:\Users\Manoj\AppData\Local\Programs\Python\Python36\lib\site-packages\
tensorflow\python\pywrap_tensorflow_internal.py", line 21, in <module>
_pywrap_tensorflow_internal = swig_import_helper()
File "C:\Users\Manoj\AppData\Local\Programs\Python\Python36\lib\site-packages\
tensorflow\python\pywrap_tensorflow_internal.py", line 20, in swig_import_helper
return importlib.import_module('_pywrap_tensorflow_internal')
File "C:\Users\Manoj\AppData\Local\Programs\Python\Python36\lib\importlib\__in
it__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
Failed to load the native TensorFlow runtime.
See https://www.tensorflow.org/install/install_sources#common_installation_probl
ems
for some common reasons and solutions. Include the entire stack trace
above this error message when asking for help.
但我得到的是以下错误消息:
file = open(filename, 'r', encoding='utf-8', errors='strict')
所以我想,让我们抓住错误,让我们以下列方式回退到'utf-8' codec can't decode byte 0xa9 in position 75: invalid start byte
:
errors=ignore
但是没有,我总是得到相同的错误信息,似乎我无法捕捉错误。我也试过了try:
file = open(filename, 'r', encoding='utf-8', errors='strict')
except UnicodeError as decode_error:
file = open(filename, 'r', encoding='utf-8', errors='ignore')
。
答案 0 :(得分:1)
打开文件无法读取。
因此,open
永远不会抛出UnicodeDecodeError
。试图读取文件。
当文件不存在(FileNotFoundError
)或没有足够的权限(PermissionError
)时,将抛出。
您必须在应用程序的其他位置捕获UnicodeDecodeError
。
话虽这么说,如果你准备无论如何都要忽略Unicode解码错误,那么首先没有理由用errors='strict'
打开文件。