Python – Errno 13 Permission denied

I need some time to figure out this error, because some references that I found suggest to check the privilages to the file or folder that I want to access, but none of the suggestion worked. And then I realized that actually I want to read a file but I provided a folder. Let’s creating an error as mean above:

Example-01: Generate the error

import pandas as pd

xlsx_file = r"C:\Downloads\Board.xlsx"
filename = r"C:\Downloads"

df = pd.read_excel(filename)

print (df)

Output:

PermissionError: [Errno 13] Permission denied: 'C:\\Downloads'

The script above generate the Errno 13 Permission denied because I give a valid folder to read as and excel file.

To avoid this problem it would be good for every script that will read a file to first check if the file exists:

Example-02: Check if the file exists:

import pandas as pd
import os

xlsx_file = r"C:\Users\dsibaran\Downloads\Board_1-nokia_danny-2023_01_18-14_38_17__977.xlsx"
filename = r"C:\Users\dsibaran\Downloads"

if os.path.isfile(filename): 
    df = pd.read_excel(filename)
    print (df)
else: 
    print("'{}' is not a file".format(filename))

Output:

'C:\Downloads' is not a file

Leave a comment