Skip to main content
 首页 » 编程设计

python之有没有办法用 Python 或任何语言手动修改 mtime 或 ctime 等统计信息

2025年05月04日11zengkefu

我正在尝试以下代码:

os.stat(path_name)[stat.ST_CTIME] = ctime 

但是,这会产生以下错误:

exceptions.TypeError: 'posix.stat_result' object does not support item assignment 

有没有办法修改ctime?

谢谢!

请您参考如下方法:

os.utime(filename, timetuple) 可用于设置文件的 atime 和 mtime。据我所知,如果不借助诸如玩时钟或直接编辑文件系统(我真的不推荐)之类的技巧,就无法从用户空间修改 ctime,这对任何编程语言都是如此(Python , Perl, C, C++...) :这是操作系统内部的东西,你不想碰它。

请参阅 touch 命令的文档 (http://www.delorie.com/gnu/docs/fileutils/fileutils_54.html) 中的示例:

Although touch provides options for changing two of the times -- the times of last access and modification -- of a file, there is actually a third one as well: the inode change time. This is often referred to as a file's ctime. The inode change time represents the time when the file's meta-information last changed. One common example of this is when the permissions of a file change. Changing the permissions doesn't access the file, so the atime doesn't change, nor does it modify the file, so the mtime doesn't change. Yet, something about the file itself has changed, and this must be noted somewhere. This is the job of the ctime field. This is necessary, so that, for example, a backup program can make a fresh copy of the file, including the new permissions value. Another operation that modifies a file's ctime without affecting the others is renaming. In any case, it is not possible, in normal operations, for a user to change the ctime field to a user-specified value.