Categories
Python Answers

How to fix PermissionError: [Errno 13] Permission denied with Python open?

Spread the love

To fix PermissionError: [Errno 13] Permission denied with Python open, we should make sure the path we call open with is a file.

For instance, we write

import os

path = r"/path/to/file.txt"
assert os.path.isfile(path)
with open(path, "r") as f:
    pass

to make sure that the path is a path to a file with os.path.isfile before we call open to open the file at the path.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *