I am trying to modify the time stamp of a file used in python.
Before running the script, I have this:
jlinkels@donald-pc:/tmp$ echo "Hello World" /tmp/filter_cc.log
jlinkels@donald-pc:/tmp$ stat filter_cc.log
Access: 2018-08-24 00:56:05.942539951 +0000
Modify: 2018-08-24 00:56:05.942539951 +0000
Change: 2018-08-24 00:56:05.942539951 +0000
That is fine. Then I run this Python script:
#! /usr/bin/python
import os, sys
os.utime ("/tmp/filter_cc.log", (1394628000.0,1394628000.0))
sys.exit()
After exiting the script I run stat again:
jlinkels@donald-pc:/tmp$ stat filter_cc.log
Access: 2018-08-24 00:58:10.441487778 +0000
Modify: 2014-03-12 12:40:00.000000000 +0000
Change: 2018-08-24 00:58:09.941491078 +0000
That is OK. The time stamp in the os.utime statement is a date in 2014.
Now this is the problem: When I access the file in the Python script, it is modified after the os.utime statement. Presumably at exit. Before running the script I delete the previous file. So this is the script:
#! /usr/bin/python
import os, sys
logfile = open ('/tmp/filter_cc.log', 'w')
logfile.write ("Hello world\n")
logfile.close
os.utime ("/tmp/filter_cc.log", (1394628000.0,1394628000.0))
sys.exit()
So I would expect that the os.utime call would set the file time to 2014. Just like in the first script. However, this is what happens:
jlinkels@donald-pc:/tmp$ stat filter_cc.log
Access: 2018-08-24 01:03:36.042778408 +0000
Modify: 2018-08-24 01:03:35.542783334 +0000
Change: 2018-08-24 01:03:35.542783334 +0000
And that is definitely not good. The last statement is the script is a os.utime. But it seems Python modifies the closed file once more on the OS level.
What should I do to close the file so that os.utime can modify the file time in this same script?
Running Debian Jessie, Python 2.7.9