Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

pandas - PermissionError: Permission denied to reading CSV File in Python

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...