我想下面的代碼:有沒有辦法用Python或任何語言手動修改像mtime或ctime這樣的統計信息?
os.stat(path_name)[stat.ST_CTIME] = ctime
然而,這提供了以下錯誤:
exceptions.TypeError: 'posix.stat_result' object does not support item assignment
反正是有修改的ctime?
謝謝!
我想下面的代碼:有沒有辦法用Python或任何語言手動修改像mtime或ctime這樣的統計信息?
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。據我所知,沒有辦法從userland修改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.
對於Windows中的一種方式,請參閱http://stackoverflow.com/q/21156145/281545 – 2017-02-12 21:10:57
沒有直接的方法來設置改變時,它就會被隨時更新inode信息變化,比如所有權,鏈接數,模式等。
嘗試將模式設置爲已設定的模式:
os.chmod(path_name, os.stat(path_name)[stat.ST_MODE])
ctime是創建時間,而不是「更改時間」。您可以任意設置mtime(和atime),而不是ctime。 – geoffspear 2011-04-27 13:16:16
@Wooble:from man fstat「time_t st_ctime;/*上次狀態更改的時間* /」 – 2011-04-27 13:28:28
@Wooble:ctime:上次狀態更改的時間[http://en.wikipedia.org/wiki/Stat_(Unix)] – 2011-12-05 20:46:30
GNU衝程實現了變化的系統用於更改文件ctime的時間技巧。如果這就是你想要的,GNU stroke會爲你做:http://stroke.sourceforge.net/。
更改系統時間,爲該文件創建一個新的硬鏈接並對其進行重命名。將系統時間更改回來。 – eumiro 2011-04-27 12:25:10
您將修改從操作系統獲得的值的副本,而不是操作系統實際使用的值。 – delnan 2011-04-27 12:25:22