PermissionError:拒绝在Python中读取CSV文件的权限 [英] PermissionError: Permission denied to reading CSV File in Python

查看:717
本文介绍了PermissionError:拒绝在Python中读取CSV文件的权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简而言之,我想在读取另一个csv文件并尝试从其中删除标头后,再读取从Data Frame转换而来的 csv 文件.

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'

我的python代码:

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.

我在Windows 10的Anaconda Spyder Editor上编码.

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!

推荐答案

在Windows中,如果您已打开CSV文件(例如在Excel中),并且您运行了脚本,则该脚本无法保存到X_Data.csv,因为该文件位于使用并提高PermissionError.

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.

关闭文件/Excel,然后尝试再次运行脚本

Close the file/Excel and try running your script again

这篇关于PermissionError:拒绝在Python中读取CSV文件的权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆