Can someone please specify what is the difference between os.path.getmtime(path)
and os.path.getctime(path)
in unix systems . As per the defnition
This is technically not a programming question and therefore shouldn't be on Stack Overflow, but you can find the answers you seek here—which happens to be the first Google result for ctime mtime atime
. Short answer: ctime
changes when the file's ownership or permissions change, as well as when the data in the file changes. mtime
changes only when the data in the file changes.
From the man page on stat, which os.path.getmtime()
and os.path.getctime()
both use on Unix systems:
The field
st_mtime
is changed by file modifications, for example, bymknod(2)
,truncate(2)
,utime(2)
andwrite(2)
(of more than zero bytes). Moreover,st_mtime
of a directory is changed by the creation or deletion of files in that directory. Thest_mtime
field is not changed for changes in owner, group, hard link count, or mode.
...The field
st_ctime
is changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).
So no, these are not the same.
The mtime refers to last time the file's contents were changed. This can be altered on unix systems in various ways. Often, when you restore files from backup, the mtime is altered to indicate the last time the contents were changed before the backup was made.
The ctime indicates the last time the inode was altered. This cannot be changed. In the above example with the backup, the ctime will still reflect the time of file restoration. Additionally, ctime is updated when things like file permissions are changed.
Unfortunately, there's usually no way to find the original date of file creation. This is a limitation of the underlying filesystem. I believe the ext4 filesystem has added creation date to the inode, and Apple's HFS also supports it, but I'm not sure how you'd go about retrieving it in Python. (The C stat
function and the corresponding stat
command should show you that information on filesystems that support it.)