1

In Python I want to delete an image if it exists before I save the image with the only goal to update the creation date of the file. A small example of the code that generates the problem:

 img = QtGui.QImage(filePath)
 os.remove(filePath)
 img.save(filePath)

This example is not exactly what I am using but when executing this the newly saved image has the creation date of the file that has just been deleted. When I put a delay between the os.remove and img.save it doesn't help (tried up to 3 seconds) but when I do a QMessagebox then the new file will actually get an updated creation date.

Why is this happening and is there anything I can do to get a new creation date on the newly saved file?

hschokker
  • 69
  • 4
  • 1
    Does [this](http://stackoverflow.com/questions/4996405/how-do-i-change-the-file-creation-date-of-a-windows-file-from-python) help? – MaTh Dec 15 '15 at 14:39
  • I am testing it now, not sure what newtime should look like.. I thought something like "time.localtime()" but then I get an error. – hschokker Dec 15 '15 at 14:53
  • Have you tried with `time.time()` ? – MaTh Dec 15 '15 at 14:57
  • 1
    The problem was somewhere else when I tried the time.localtime(), it might actually have worked. I just answered my own question with help from the link you posted, thanks. – hschokker Dec 15 '15 at 15:19

1 Answers1

1

Below is what worked for me, I made a small adjustment from the example given in the link of the comment by Drjnkr. The utc date/time is now set as the creation date. I still don't know why os.remove(filename) doesn't delete the creation date of the file when directly afterwards I recreate the file but this also works for me.

def changeFileCreationTime(self, fname):
    now_utc = win32timezone.utcnow()
    winfile = win32file.CreateFile(
    fname, win32con.GENERIC_WRITE,
    win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE |win32con.FILE_SHARE_DELETE,
    None, win32con.OPEN_EXISTING,
    win32con.FILE_ATTRIBUTE_NORMAL, None)
    win32file.SetFileTime(winfile, now_utc, now_utc, now_utc)
    winfile.close()
hschokker
  • 69
  • 4