I try to open the text file and it does not work
with open('quiz.txt') as f:
lines = f.readlines()
Traceback (most recent call last):
File "<pyshell#35>", line 2, in <module>
lines=f.readlines()
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 168: invalid continuation byte
rdas
21.4k6 gold badges39 silver badges48 bronze badges
-
Does this answer your question? How do I open a text file in Python?deikyb– deikyb2020年04月20日 19:12:32 +00:00Commented Apr 20, 2020 at 19:12
1 Answer 1
Invalid continuation byte = not unicode = probably a binary file.
with open('quiz.txt', 'rb') as f:
lines = f.readlines()
will open the file in bytes mode.
Another possibility is that you are executing this in your shell, and the program looks for stuff only in the working directory.
import os
os.chdir('/path/to/your/file/excluding/file/name')
with open('quiz.txt', 'rb') as f:
lines = f.readlines()
answered Apr 20, 2020 at 19:12
Eric Jin
3,9144 gold badges23 silver badges48 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-py