1

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

  • You are so right. Apparently Python accepts this without generating an error. Why is it a valid statement anyway even if it doesn't do what is expected? (At least not by me?). If you post this as an answer I can upvote it. – Johannes Linkels Aug 24 '18 at 01:42
  • ok. i will add that with an explaination. – e4c5 Aug 24 '18 at 01:47

1 Answers1

2

The problem here is that you have forgotten to close the file.

logfile.close 

To close the file you need to do

logfile.close()

Even though the first code fragment wasn't what you wanted it does not produce an error because it's still valid syntax. Functions are just like any variables. In other words functions are first class objects. There is an interesting discussion of it here: What are "first class" objects?

So any where in your code, if you add a name of a function that want to call without adding the parenthesis, the python interpreter will believe that you wanted to create an expression and carry on without producing an error.

e4c5
  • 52,766
  • 11
  • 101
  • 134
  • 1
    I am just a beginner in Python. Although I have 40 years of programming experience in many languages, this is the first time I encounter a case where leaving out parenthesis does not generate an error. I understand what you say, it just never dawned to me this was possible. – Johannes Linkels Aug 24 '18 at 02:04
  • glad to have been of help. Yes, if you come from a language like Java this can realy trip you up – e4c5 Aug 24 '18 at 02:05