pandas -快速读取/写入相同的csv ..获取权限错误 [英] Pandas - Read/Write to the same csv quickly.. getting permissions error

查看:53
本文介绍了 pandas -快速读取/写入相同的csv ..获取权限错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我试图每2秒执行一次..开始时,它将用 pd.read_csv 读取 .csv .然后在df上执行修改,最后用 to_csv 覆盖原始的 .csv .

I have a script that I am trying to execute every 2 seconds.. to begin it reads a .csv with pd.read_csv. Then executes modifications on the df and finally overwrites the original .csv with to_csv.

我遇到了 PermissionError:[Errno 13]权限被拒绝:,从我的搜索中,我相信这是由于尝试打开/写入同一文件太频繁而导致的,尽管我可能错了

I'm running into a PermissionError: [Errno 13] Permission denied: and from my searches I believe it's due to trying to open/write too often to the same file though I could be wrong.

  • 有人建议如何避免这种情况?
  • 不确定是否相关,但文件存储在单驱动器文件夹中.
  • 它确实偶尔会保存,看似随机.
  • 增加超时时间以使脚本执行速度较慢,这对我有帮助,但我希望它能快速运行!

谢谢

推荐答案

由于您没有共享确切的代码,因此我们只能假定您像这样存储数据框:

Since you don't share your exact code, we can only assume that you store your dataframe like this:

df.to_csv("myfile.csv", sep = ",", index = False)    # Drop to csv w/o context manager

在这种情况下,您遇到的行为是由于文件未正确关闭.这是一个常见的错误.我建议使用 with -声明,其主要用途是对内部使用的对象(在本例中为 .csv )进行异常安全清除.换句话说, with 可确保关闭文件,释放锁,还原上下文等.

In this case, the behavior you are experiencing is due the file not being closed properly. This is a common mistake. I recommend to use the with-statement, whose primary use is an exception-safe cleanup of the object used inside (in this case your .csv). In other words, with makes sure that files are closed, locks released, contexts restored etc.

with open("myfile.csv", "w") as reference:           # Drop to csv w/ context manager
     df.to_csv(reference, sep = ",", index = False)
# As soon as you are here, reference is closed

这篇关于 pandas -快速读取/写入相同的csv ..获取权限错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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