I'm currently working on a project where I write output from a pandas dataframe to a .csv file at each iterative step in the process of running a simulation. However, seemingly at random, the program will break and I will get the error:
[Errno 13] Permission denied: 'C:\Users\samue\Desktop\Lab Python\nm12_diam1_pen100000_lam1.5_e-2_a1.75_dt0.0001_rt1000000_T300_drag1_k100_confFalse_2020-09-11 23_11_53.110105.csv'
This is especially weird because it seems to work for anywhere from 500 to 500,000 iterative steps before I get this error message--it seems completely random. Here's the code I'm using to write output:
#name = name of .csv file
path = os.getcwd()
full_name = path + '\\' + name
#if time-point = 0, write the file
if t[0]==0:
df.to_csv(full_name, mode='w', index=False)
#if time-point != 0, append the file with new data
else:
with open(full_name, 'a+') as f:
df.to_csv(f, mode='a+', header=False, index=False, encoding="utf-16")
f.close()
I've checked if this is an error with having a read-only folder as the location to which I'm writing the file, but this isn't the case. It's also not an issue with the way that I'm naming the the .csv file (I've tried shorter names (e.g. test.csv) and it doesn't fix the issue).
I also thought it might be an issue of not closing the file after opening it, so I've added f.close(), but this hasn't made it work any more consistently.
I've also tried changing the mode from 'a' to 'a+' and it seems to be functionally the same.
Does anyone have any ideas?