0

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

enter image description here

rdas
  • 20,604
  • 6
  • 33
  • 46
ziqi yin
  • 1
  • 2
  • Does this answer your question? [How do I open a text file in Python?](https://stackoverflow.com/questions/40096612/how-do-i-open-a-text-file-in-python) – dawis11 Apr 20 '20 at 19:12

1 Answers1

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()
Eric Jin
  • 3,836
  • 4
  • 19
  • 45