Reputation: 6992
I was hoping that just using something like
with open(file_name, "w") as f:
would not change ctime if the file already existed. Unfortunately it does. Is there a version which will leave the ctime intact?
Motivation: I have a file that contains a list of events. I would like to know how old the oldest event is. It seems this should be the files ctime.
Upvotes: 0
Views: 1542
Reputation: 1
Motivation: I have a file that contains a list of events. I would like to know how old the oldest event is. It seems this should be the files ctime.
This is a perfect use case for a sqlite database (or perhaps even a PostGreSQL one, if your application can be run either on several Linux hosts -sharing a common database server- or in different Linux processes), or at least a GDBM indexed file. BTW, what exactly is an event for your application, and how is each event represented in the file? If you use any relational database, invest your efforts on designing well enough the database schema, learn about database normalization and cleverly design suitable database indexes.
And I would register each event in that file or database with an explicit event addition time. See time(7) for more.
Maybe you are considering a huge volume of data (many terabytes). Then look also into this answer.
Be aware that your processor is a lot faster than your disk (even an SSD one). In practice, a significant part of your file data may sit in the page cache (so getting more RAM could improve performance significantly).
See also https://www.linuxatemyram.com/ and http://norvig.com/21-days.html for useful insights.
If performance really matters to you, consider recoding your application in some compiled language implementation (C++ with GCC, Rust, Ocaml, SBCL, Go....). Most of them are significantly faster than Python.
Be aware that disk space is cheaper than CPU time, which is cheaper than your developer's time and efforts.
Upvotes: -1
Reputation: 149165
Beware, ctime is not the creation time but the inode change time. It is updated each time you write to the file, or change its meta-data, for example rename it. So we have:
I know no way to reset the ctime field because even utimes
and its variant can only set the atime and mtime (and birthtime for file systems that support it like BSD UFS2) - except of course changing the system time with all the involved caveats...
Upvotes: 3
Reputation: 2083
Unfortunately there isn't. All you can do to a file without changing its ctime is to read it. All you could do is setting the ctime, although is is not trivial either. You have to change your systems time, then touch the file and then reset your system time to current time.
I'm talking about Unix Systems, as OP stated in the tags
Upvotes: 0
Reputation: 483
Because fopen
works that way when using 'w' as an option. From the manual:
"w" write: Create an empty file for output operations.
If a file with the same name already exists, its contents are discarded and the file is treated as a new empty file.
If you don't want to create a new file use a+
to append to the file. This leaves the create date intact.
Upvotes: 1