11

Simply I wanted to read the csv file that I converted from Data Frame after read the another csv file and same time I tried to remove header from in it.

Then I got the error below one :

PermissionError: [Errno 13] Permission denied: 'X_Data.csv'

My python code :

import pandas as pd
import numpy as np

df = pd.read_csv('input_doc.csv').replace(' ?', np.nan).dropna()

data_X = df.iloc[:, 1:15].values 
data_Y = df.iloc[:, :1].values 

clean_X = pd.DataFrame(data_X);
clean_Y = pd.DataFrame(data_Y);

clean_X.to_csv("X_Data.csv", index=False)
clean_Y.to_csv("Y_Data.csv", index=False)

X = pd.read_csv("X_Data.csv", encoding="utf-8", header=1)
Y = pd.read_csv("Y_Data.csv", encoding="utf-8", header=1)

Also I got the same error without removing header when reading.

I found several issues that similar to my problem , but those won't fix my problem.

I coded on Anaconda Spyder Editor in Windows 10.

How can I read this file without getting this error? What am I missing?

Thank you so much! Any Help would be appreciated!

Sarasa Gunawardhana
  • 1,099
  • 3
  • 14
  • 34
  • Possible duplicate:https://stackoverflow.com/questions/13207450/permissionerror-errno-13-in-python – Sandesh Gupta Apr 29 '18 at 06:05
  • @prabhakar should I need to give permission manually? I'm working one windows enviroment using spyder editor. – Sarasa Gunawardhana Apr 29 '18 at 06:13
  • I don't know what you mean by "manually", but yes, of course the user running the code needs permissions to use the file. The IDE you are using is irrelevant, what's important is the user you are running under. – cdarke Apr 29 '18 at 06:33
  • What is the version of spyder you are using? – prabhakar Apr 29 '18 at 06:43
  • @cdarke I have Full Administrator access. better If I can do something by coding. so it seems I have to change permission on file using file properties – Sarasa Gunawardhana Apr 29 '18 at 06:49
  • @prabhakar version no 3.2.8 – Sarasa Gunawardhana Apr 29 '18 at 06:50
  • OK, but do you select "Run as Administrator" when you run your environment? Just because you are an administrator does not mea you automatically have all the rights. Its a wonder of Windows. – cdarke Apr 29 '18 at 06:56
  • There seem to be a [bug](https://github.com/spyder-ide/spyder/issues/5209) in spyder version 3.2.1 or below, It was supposed to be fixed in 3.2.2. Not sure if this has resurfaced again. Why dont you check on the windows command prompt to run it with say `python3 filename.py` and check if it works? – prabhakar Apr 29 '18 at 06:56
  • Possible duplicate of [PermissionError: \[Errno 13\] in python](https://stackoverflow.com/questions/13207450/permissionerror-errno-13-in-python) – Jeru Luke Jul 11 '18 at 13:57

6 Answers6

33

In Windows, if you have the CSV file open (e.g. in Excel), and you run the script it cannot save to X_Data.csv since the file is in use and raises the PermissionError.

Close the file/Excel and try running your script again

Anjum Sayed
  • 872
  • 9
  • 20
7

I think the User you are using to run the python file does not have Read (or if you want to change file and save it Write) permission over CSV file or it's directory.

If you are on Linux use CHMOD command to grant access the file:

public access: chmod 777 csv_file

And if you are on Windows change privacy and permissions of file and folder.

1

In my case, I have not opened the file exactly but I have uploaded the same file in another application and that is the issue. So it was not allowed to read that file from another application. Once I close/refresh other application, python application able to read without any permission error.

Chitta
  • 185
  • 2
  • 15
1

Whatever the other folks described is probably correct.

In my particular case:

The text file I am trying to read is on a shared drive on my work laptop. Corporations sync the shared drive so that you don't lose your data. With me this sync was the issue. I manually synced the folder and then I was able to read the file.

If the file is not synced, I doubt if you can even open the file. I tried and it said "Permission Denied" as it shows in jupyter notebook.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Arpit
  • 333
  • 2
  • 9
0

Most of the time the issue is leaving the file open, However I came across this when the file path was set wrong, for eg. my data was compressed, so on decompressing the csv file was saved inside a folder with the same name i.e. "File_Name/File_Name.csv" in my path directory. reading the csv file without entering the "File_Name" directory caused this error to show up.

0

If you are a Windows user, have OneDrive set up on your system, and your code path by any chance is on OneDrive, you will encounter this error if your OneDrive sync is on!

Cause:

Your code saves a csv file in the code path. OneDrive tries to sync the file (locking access to the csv file). After that, your code tries to open the file while sync is in progress. Access to csv file is denied by windows and your code throws this exception.

Two possible solutions:

  1. Move your code directory out of OneDrive.
  2. Or turn sync off while your code is running and turn it back on when you want to save your work.
tdy
  • 36,675
  • 19
  • 86
  • 83