I am having a strange issue with python when I try to change the "Date Modified" field value in Windows. Specifically, if I write to the file and then try to change its Date Modified attribute, the change fails (the Date Modified becomes the time I ran the python script). However, if I don't write to the file (if I comment out the out.write function call) the Date Modified time is correctly set to 11/2017.
The Date Accessed time is set as expected in both situations.
Below is my code (python 2.7):
import os
import time
import datetime
out = open("out.test", "wb")
#comment out the write line to get this to work
out.write("hi")
out.flush()
out.close
fileLocation = "out.test"
year = 2017
month = 11
day = 5
hour = 19
minute = 50
second = 0
date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
modTime = time.mktime(date.timetuple())
os.utime(fileLocation, (modTime, modTime))
Interestingly, if I use a separate python process (i.e. run python in another cmd prompt) to alter the Date Modified, that also works as well.
Is there some other flush-like function I need to call in order to properly set the Date Modified time if I write to the file?
Thanks in advance.