pandas PermissionError:权限被拒绝在 Python 中读取 CSV 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50083806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PermissionError: Permission denied to reading CSV File in Python
提问by Sarasa Gunawardhana
Simply I wanted to read the csvfile that I converted from Data Frame after read the another csv file and same time I tried to remove header from in it.
只是我想读的CSV文件,我从数据帧转换读取另一个CSV文件,同时,我试图从它删除头后。
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.
我在 Windows 10 中的 Anaconda Spyder Editor 上编码。
How can I read this file without getting this error? What am I missing?
如何在不出现此错误的情况下读取此文件?我错过了什么?
Thank you so much! Any Help would be appreciated!
非常感谢!任何帮助,将不胜感激!
回答by Anjum Sayed
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
.
在 Windows 中,如果您打开了 CSV 文件(例如在 Excel 中),并且您运行该脚本,则该脚本无法保存,X_Data.csv
因为该文件正在使用中并引发PermissionError
.
Close the file/Excel and try running your script again
关闭文件/Excel 并尝试再次运行您的脚本
回答by Seyed Mohammad Hosseini
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.
我认为您用来运行 python 文件的用户没有对 CSV 文件或其目录的读取(或者如果您想更改文件并将其保存为写入)权限。
If you are on Linux use CHMOD command to grant access the file:
如果您在 Linux 上使用 CHMOD 命令授予访问文件的权限:
public access: chmod 777 csv_file
公共访问: chmod 777 csv_file
And if you are on Windows change privacy and permissions of file and folder.
如果您使用的是 Windows,请更改文件和文件夹的隐私和权限。
回答by Chitta
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.
就我而言,我没有完全打开文件,但我在另一个应用程序中上传了相同的文件,这就是问题所在。因此不允许从另一个应用程序读取该文件。一旦我关闭/刷新其他应用程序,python 应用程序就可以在没有任何权限错误的情况下读取。
回答by Arpit
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 loose 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.
如果文件未同步,我怀疑您是否甚至可以打开文件。我试过了,它在 jupyter notebook 中显示“权限被拒绝”。
Hope that helps.
希望有帮助。