当 pandas 数据帧到tempfile csv时权限被拒绝 [英] Permission denied when pandas dataframe to tempfile csv

查看:58
本文介绍了当 pandas 数据帧到tempfile csv时权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以csv格式(在Windows中)将熊猫数据框存储到tempfile中,但遭到以下攻击:

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

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

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

import tempfile
import pandas

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

其中df是数据帧.我还尝试过将temp目录更改为一个,我确信我具有写权限:

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

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

这给了我同样的错误信息

This gives me the same error message

当我将循环更改为以下内容时,临时文件似乎已被锁定以进行

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')

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

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.

推荐答案

检查您的权限,并根据帖子,您可以通过右键单击以管理员身份运行程序并以管理员身份运行.

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

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

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

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

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

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.

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

参考

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

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

使用此功能可能会在程序中引入安全漏洞.当您开始使用返回的文件名执行任何操作时,可能有人被您打败了.可以轻松地将 mktemp()用法替换为 NamedTemporaryFile(),并将其传递给 delete = False 参数.

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=False paramete.

了解详情.

导出为 CSV 后,您可以使用 temp.close()关闭文件.

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

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

这篇关于当 pandas 数据帧到tempfile csv时权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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