Difference between python - getmtime() and getctime() in unix system

后端 未结 3 778
终归单人心
终归单人心 2020-12-09 08:43

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

相关标签:
3条回答
  • 2020-12-09 09:01

    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.

    0 讨论(0)
  • 2020-12-09 09:08

    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, by mknod(2), truncate(2), utime(2) and write(2) (of more than zero bytes). Moreover, st_mtime of a directory is changed by the creation or deletion of files in that directory. The st_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.

    0 讨论(0)
  • 2020-12-09 09:18

    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.)

    0 讨论(0)
提交回复
热议问题