LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2018, 08:08 PM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Python modifies a closed file at program exit


I am trying to modify the time stamp of a file used in python.

Before running the script, I have this:
Code:
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:
Code:
#! /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:
Code:
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:
Code:
#! /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:
Code:
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.

Running Debian Jessie, Python 2.7.9

jlinkels
 
Old 08-23-2018, 08:44 PM   #2
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Solved. The file was not closed at all.
Code:
logfile.close
instead of
Code:
logfile.close()
jlinkels
 
Old 08-23-2018, 09:59 PM   #3
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,226

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The pattern to use is:

Code:
with open ('/tmp/filter_cc.log', 'w') as logfile:
    logfile.write ("Hello world\n")
That ensures that the file is closed no matter what.
 
2 members found this post helpful.
Old 08-24-2018, 07:32 AM   #4
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Original Poster
Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
I just started in Python. I think I am still too much used to old school, open(), write() and close(). And also the expectation that Python would generate an error when stating close instead of close(). I have "some" programming experience, back to Algol 60 and Fortran IV, so this assumptions might work against me. Never mind, one is never too old to learn.

jlinkels
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Path to new file in Python program on Linux Dylan_Oscar Programming 5 11-10-2017 03:39 PM
[SOLVED] How to: tkinter-python to monitor a file, add buttons automatically, and exit cleanly donnied Programming 0 10-13-2012 01:37 PM
bash: Can I pipe the name of a file created by a program to a python script? SparceMatrix Programming 4 01-07-2012 11:38 AM
exit locked 'getline' if pipe is closed ta0kira Programming 1 04-02-2007 11:10 AM
Problem with sftp! (debug1: Exit status 127 Connection closed) nena Linux - Security 1 01-26-2006 07:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration