Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PermissionError: Permission denied to reading CSV File in Python

Tags:

python

pandas

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!

like image 285
Sarasa Gunawardhana Avatar asked Apr 29 '18 05:04

Sarasa Gunawardhana


3 Answers

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

like image 173
Anjum Sayed Avatar answered Sep 18 '22 15:09

Anjum Sayed


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.

like image 28
Seyed Mohammad Hosseini Avatar answered Sep 20 '22 15:09

Seyed Mohammad Hosseini


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.

like image 30
Chitta Avatar answered Sep 18 '22 15:09

Chitta