python - unix 系统中的 getmtime() 和 getctime() 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17958987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:35:57  来源:igfitidea点击:

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

pythonpython-2.6

提问by misguided

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 in python docs:

有人可以指定Unix 系统os.path.getmtime(path)os.path.getctime(path)Unix 系统之间的区别吗?根据 python 文档中的定义:

os.path.getmtime(path)

os.path.getmtime(path)

Return the time of last modification of path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise os.error if the file does not exist or is inaccessible.

返回上次修改路径的时间。返回值是一个数字,给出自纪元以来的秒数(参见时间模块)。如果文件不存在或无法访问,则引发 os.error 。

os.path.getctime(path)

os.path.getctime(path)

Return the system's ctime which, on some systems (like Unix) is the time of the last change, and, on others (like Windows), is the creation time for path. The return value is a number giving the number of seconds since the epoch (see the time module). Raise os.error if the file does not exist or is inaccessible.

返回系统的 ctime,在某些系统(如 Unix)上是最后一次更改的时间,而在其他系统(如 Windows)上是路径的创建时间。返回值是一个数字,给出自纪元以来的秒数(参见时间模块)。如果文件不存在或无法访问,则引发 os.error 。

Does that basically mean they are the same things when used in unix/systems?

这是否意味着它们在 unix/systems 中使用时是相同的东西?

#!/usr/bin/python
import os
print os.path.getmtime('File')
print os.path.getctime('FIle')

Both the prints fetch me the same value.

两个打印件都为我获取了相同的值。

I am basically looking for last creation date for file , rather than last modification date. Is there a way to achieve the same in unix?

我基本上是在寻找 file 的最后创建日期,而不是最后修改日期。有没有办法在unix中实现相同的目标?

采纳答案by eaj

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.

mtime 是指上次更改文件内容的时间。这可以在 Unix 系统上以各种方式改变。通常,当您从备份中恢复文件时,mtime 会更改以指示在进行备份之前内容的最后一次更改时间。

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.

ctime 表示上次更改 inode 的时间。这是无法改变的。在上面带有备份的示例中,ctime 仍将反映文件恢复的时间。此外,当更改文件权限等内容时,ctime 也会更新。

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 statfunction and the corresponding statcommand should show you that information on filesystems that support it.)

不幸的是,通常无法找到文件创建的原始日期。这是底层文件系统的限制。我相信 ext4 文件系统已经为 inode 添加了创建日期,Apple 的 HFS 也支持它,但我不确定你会如何在 Python 中检索它。(Cstat函数和相应的stat命令应该向您显示有关支持它的文件系统的信息。)

回答by kindall

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: ctimechanges when the file's ownership or permissions change, as well as when the data in the file changes. mtimechanges only when the data in the file changes.

这在技术上不是编程问题,因此不应该出现在 Stack Overflow 上,但您可以在这里找到您想要的答案——这恰好是ctime mtime atime. 简短回答:ctime当文件的所有权或权限发生变化时,以及文件中的数据发生变化时也会发生变化。mtime仅当文件中的数据发生变化时才会发生变化。

回答by Andrew Clark

From the man page on stat, which os.path.getmtime()and os.path.getctime()both use on Unix systems:

来自 stat 的手册页,它os.path.getmtime()os.path.getctime()两者都在 Unix 系统上使用:

The field st_mtimeis changed by file modifications, for example, by mknod(2), truncate(2), utime(2)and write(2)(of more than zero bytes). Moreover, st_mtimeof a directory is changed by the creation or deletion of files in that directory. The st_mtimefield is not changed for changes in owner, group, hard link count, or mode.
...

The field st_ctimeis changed by writing or by setting inode information (i.e., owner, group, link count, mode, etc.).

该字段 st_mtime因文件修改而改变,例如,由mknod(2)truncate(2)utime(2)write(2)(超过零字节)。此外,st_mtime通过在该目录中创建或删除文件来更改目录。该st_mtime字段不会因所有者、组、硬链接计数或模式的更改而更改。
...

st_ctime通过写入或设置 inode 信息(即所有者、组、链接计数、模式等)来更改该字段。

So no, these are not the same.

所以不,这些是不一样的。