Python 当熊猫数据框到临时文件 csv 时权限被拒绝

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43283202/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:47:13  来源:igfitidea点击:

Permission denied when pandas dataframe to tempfile csv

pythoncsvpandastemporary-files

提问by thebigspin

I'm trying to store a pandas dataframe to a tempfile in csv format (in windows), but am being hit by:

我正在尝试将 Pandas 数据帧存储到 csv 格式的临时文件中(在 Windows 中),但被以下情况击中:

[Errno 13] Permission denied: 'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'

[Errno 13] 权限被拒绝:'C:\Users\Username\AppData\Local\Temp\tmpweymbkye'

import tempfile
import pandas

with tempfile.NamedTemporaryFile() as temp:
    df.to_csv(temp.name)

Where df is the dataframe. I've also tried changing the temp directory to one I am sure I have write permissions:

其中 df 是数据框。我还尝试将临时目录更改为一个我确信我有写权限的目录:

tempfile.tempdir='D:/Username/Temp/'

This gives me the same error message

这给了我同样的错误信息

Edit:

编辑:

The tempfile appears to be locked for editing as when I change the loop to:

当我将循环更改为:

with tempfile.NamedTemporaryFile() as temp:
    df.to_csv(temp.name + '.csv')

I can write the file in the temp directory, but then it is not automatically deleted at the end of the loop, as it is no longer a temp file.

我可以将文件写入临时目录,但是在循环结束时它不会自动删除,因为它不再是临时文件。

However, if I change the code to:

但是,如果我将代码更改为:

with tempfile.NamedTemporaryFile(suffix='.csv') as temp:
    training_data.to_csv(temp.name)

I get the same error message as before. The file is not open anywhere else.

我收到与以前相同的错误消息。该文件未在其他任何地方打开。

采纳答案by RaminNietzsche

Check your permissions and, according to thispost, you can run your program as an administrator by right click and run as administrator.

检查您的权限,根据这篇文章,您可以通过右键单击并以管理员身份运行以管理员身份运行您的程序。

We can use the to_csv command to do export a DataFrame in CSV format. Note that the code below will by default save the data into the current working directory. We can save it to a different folder by adding the foldername and a slash to the file

我们可以使用 to_csv 命令以 CSV 格式导出 DataFrame。请注意,下面的代码默认将数据保存到当前工作目录中。我们可以通过在文件中添加文件夹名和斜杠将其保存到不同的文件夹

verticalStack.to_csv('foldername/out.csv').

Check out your working directory to make sure the CSV wrote out properly, and that you can open it! If you want, try to bring it back into python to make sure it imports properly.

检查您的工作目录以确保 CSV 正确写出,并且您可以打开它!如果需要,请尝试将其带回 python 以确保其正确导入。

newOutput = pd.read_csv('out.csv', keep_default_na=False, na_values=[""])

ref

参考

Unlike TemporaryFile(), the user of mkstemp()is responsible for deleting the temporary file when done with it.

与 不同TemporaryFile(), 的用户mkstemp()负责在完成后删除临时文件。

With the use of this function may introduce a security hole in your program. By the time you get around to doing anything with the file name it returns, someone else may have beaten you to the punch. mktemp()usage can be replaced easily with NamedTemporaryFile(), passing it the delete=Falseparamete.

使用此函数可能会在您的程序中引入安全漏洞。当您开始使用它返回的文件名做任何事情时,其他人可能已经打败了您。mktemp()用法可以很容易地替换为NamedTemporaryFile(),将delete=False参数传递给它。

Read more.

阅读更多

After export to CSVyou can close your file with temp.close().

导出后,CSV您可以使用temp.close().

with tempfile.NamedTemporaryFile(delete=False) as temp:
    df.to_csv(temp.name + '.csv')
    temp.close()

回答by hm6

I encountered the same error message and the issue was resolved after adding "/df.csv" to file_path.

我遇到了同样的错误消息,在将“/df.csv”添加到 file_path 后问题得到解决。

df.to_csv('C:/Users/../df.csv', index = False)

回答by prathik shirolkar

Sometimes, it gives that error simply because there is another file with the same name and it has no permission to delete the earlier file and replace it with the new file.

有时,它只是因为有另一个同名文件而导致该错误,并且它无权删除较早的文件并将其替换为新文件。

  1. So either name the file differently while saving it, or
  2. If you are working on Jupyter Notebook or a other similar environment, delete the file after executing the cell that reads it into memory. So that when you execute the cell which writes it to the machine, there is no other file that exists with that name.
  1. 因此,要么在保存文件时以不同的方式命名文件,要么
  2. 如果您正在使用 Jupyter Notebook 或其他类似环境,请在执行将文件读入内存的单元后删除该文件。因此,当您执行将其写入机器的单元时,不存在具有该名称的其他文件。

回答by saneryee

Sometimes,you need check the file path that if you have right permission to read and write file. Especially when you use relative path.

有时,您需要检查文件路径是否具有读写文件的正确权限。特别是当您使用相对路径时。

xxx.to_csv('%s/file.csv'%(file_path), index = False)

回答by Shaurya Singh

Just give a valid path and a file name

只需给出一个有效的路径和文件名

e.g:

例如:

final_df.to_csv('D:\Study\Data Science\data sets\MNIST\sample.csv')