1

I try to overwrite a figure with matplotlib together with os.remove() using below code. Why is the creation date not updated? (I am using Anaconda Prompt)

the code is source from here: Matplotlib Savefig will NOT overwrite old files

some suggest using os.remove(os.path.join(path, del_file)) as here:Python os.remove fails to remove But that didn't work for me.

Is there a way to code up something to update the creation date?

import os
import matplotlib.pyplot as plt
#make figure
plt.plot([1,2,3,4])
plt.ylabel('some numbers')

#save
strFile = "./figs/my_plot.jpg"
if os.path.isfile(strFile):
   os.remove(strFile)   # Opt.: os.system("rm "+strFile)
plt.savefig(strFile)

Edits: I am using windows system.

wwj123
  • 365
  • 2
  • 12

2 Answers2

0

I am getting the same strange behavior writing to google drive folder on my windows machine. I use os.remove and then using pickle to save file using 'w' (which I can confirm the content changes).

It works if i delete it and put time.sleep(30) after the deletion. I assume the cloud drive is catching up, hopefully this helps you, I am not sure what the time needed for it to catch up, likely far below 30 seconds but 30 should be plenty of time so its a safe bet.

Phillip Maire
  • 323
  • 2
  • 10
0

Here is a work around, that worked for me:

https://stackoverflow.com/a/4996407/10571437

with

import time
newtime = time.time()
Rebecca
  • 73
  • 2
  • 9