我有一个脚本,我试图每2秒执行一次..开始时会用.csv
读取pd.read_csv
。然后对df执行修改,最后用.csv
覆盖原始to_csv
。
我遇到了PermissionError: [Errno 13] Permission denied:
并且从我的搜索开始,我认为这是因为我试图经常打开/写入同一个文件,但我可能错了。
由于
答案 0 :(得分:2)
由于您不能共享您的确切代码,我们只能假设您存储的数据框如下:
df.to_csv("myfile.csv", sep = ",", index = False) # Drop to csv w/o context manager
在这种情况下,您遇到的行为是由于文件没有正确关闭。这是一个常见的错误。我建议使用with
- statement,其主要用途是对内部使用的对象(在本例中为.csv
)进行异常安全清理。换句话说,with
确保文件关闭,锁定释放,上下文恢复等。
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
答案 1 :(得分:-1)
Close the file that you are trying to read and write and then try running your script. Hope it helps