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

1

1 Answer 1

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.