Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file creation & modification date/times?

Tags:

python

file

I have a script that needs to do some stuff based on file creation and modification dates, but it has to run on Linux and Windows.

What's the best cross-platform way to get file creation and modification date/times in Python?

like image 752
Mark Biek Avatar asked Oct 25 '08 21:10

Mark Biek


People also ask

How do I find the creation time of a file?

To find a file creation date and time “crtime” is to find the inode of the file using the stat command against a file called “About-TecMint”. Alternatively, you can use the ls -i command against a file called “About-TecMint”. From the output of the above commands, the file inode number is 14420015.

How can I find the date of a file?

You can use this code to see the last modified date of a file. DateTime dt = File. GetLastWriteTime(path);


2 Answers

Getting some sort of modification date in a cross-platform way is easy - just call os.path.getmtime(path) and you'll get the Unix timestamp of when the file at path was last modified.

Getting file creation dates, on the other hand, is fiddly and platform-dependent, differing even between the three big OSes:

  • On Windows, a file's ctime (documented at https://msdn.microsoft.com/en-us/library/14h5k7ff.aspx) stores its creation date. You can access this in Python through os.path.getctime() or the .st_ctime attribute of the result of a call to os.stat(). This won't work on Unix, where the ctime is the last time that the file's attributes or content were changed.
  • On Mac, as well as some other Unix-based OSes, you can use the .st_birthtime attribute of the result of a call to os.stat().
  • On Linux, this is currently impossible, at least without writing a C extension for Python. Although some file systems commonly used with Linux do store creation dates (for example, ext4 stores them in st_crtime) , the Linux kernel offers no way of accessing them; in particular, the structs it returns from stat() calls in C, as of the latest kernel version, don't contain any creation date fields. You can also see that the identifier st_crtime doesn't currently feature anywhere in the Python source. At least if you're on ext4, the data is attached to the inodes in the file system, but there's no convenient way of accessing it.

    The next-best thing on Linux is to access the file's mtime, through either os.path.getmtime() or the .st_mtime attribute of an os.stat() result. This will give you the last time the file's content was modified, which may be adequate for some use cases.

Putting this all together, cross-platform code should look something like this...

import os import platform  def creation_date(path_to_file):     """     Try to get the date that a file was created, falling back to when it was     last modified if that isn't possible.     See http://stackoverflow.com/a/39501288/1709587 for explanation.     """     if platform.system() == 'Windows':         return os.path.getctime(path_to_file)     else:         stat = os.stat(path_to_file)         try:             return stat.st_birthtime         except AttributeError:             # We're probably on Linux. No easy way to get creation dates here,             # so we'll settle for when its content was last modified.             return stat.st_mtime 
like image 126
Mark Amery Avatar answered Oct 26 '22 21:10

Mark Amery


You have a couple of choices. For one, you can use the os.path.getmtime and os.path.getctime functions:

import os.path, time print("last modified: %s" % time.ctime(os.path.getmtime(file))) print("created: %s" % time.ctime(os.path.getctime(file))) 

Your other option is to use os.stat:

import os, time (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file) print("last modified: %s" % time.ctime(mtime)) 

Note: ctime() does not refer to creation time on *nix systems, but rather the last time the inode data changed. (Thanks to kojiro for making that fact more clear in the comments by providing a link to an interesting blog post.)

like image 22
Bryan Oakley Avatar answered Oct 26 '22 21:10

Bryan Oakley