Skip to content Skip to sidebar Skip to footer

Pandas - Read/write To The Same Csv Quickly.. Getting Permissions Error

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 .

Solution 1:

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

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.

withopen("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

Solution 2:

Post a Comment for "Pandas - Read/write To The Same Csv Quickly.. Getting Permissions Error"