在linux上使用Python获取文件创建时间

2024-06-13 11:27:19 发布

您现在位置:Python中文网/ 问答频道 /正文

stat返回stúmtime和stúctime属性,修改时间为POSIX上的st戋mtime和st戋ctime“更改时间”。 有没有函数可以返回使用python和Linux创建文件的时间?


Tags: 文件函数属性linux时间statposixst
3条回答

You probablycan't.

3.1) How do I find the creation time of a file?

You can't - it isn't stored anywhere. Files have a last-modified time (shown by "ls -l"), a last-accessed time (shown by "ls -lu") and an inode change time (shown by "ls -lc"). The latter is often referred to as the "creation time" - even in some man pages - but that's wrong; it's also set by such operations as mv, ln, chmod, chown and chgrp.

The man page for "stat(2)" discusses this.

尝试:

st_birthtime

但它并不能保证在所有系统上都可用。从文档中:

On some Unix systems (such as Linux), the following attributes may also be available: st_blocks (number of blocks allocated for file), st_blksize (filesystem blocksize), st_rdev (type of device if an inode device). st_flags (user defined flags for file).

On other Unix systems (such as FreeBSD), the following attributes may be available (but may be only filled out if root tries to use them): st_gen (file generation number), st_birthtime (time of file creation).

http://docs.python.org/2/library/os.html#os.stat

根据线程hereOS X的HFS和微软的NTFS也都跟踪出生时间,我被告知osx和Cygwin版本的stat()返回此信息。查看osx stat manpage似乎至少对mac是正确的:

a、m、c、B

The time file was last accessed or modified, of when the inode was last changed, or the birth time of the inode.

对于像ext4、Btrfs和JFS这样使用debugfs的linux新文件系统,有一个从here中获取的bash函数将提取创建日期的时间戳:

You may recover the file creation date if you deal with capable filesystem like EXT4 - journaling file system for Linux:

Improved timestamps

... Ext4 provides timestamps measured in nanoseconds. In addition, ext4 also adds support for date-created timestamps. But there no consensus in the community on that so

... as Theodore Ts'o points out, while it is easy to add an extra creation-date field in the inode (thus technically enabling support for date-created timestamps in ext4), it is more difficult to modify or add the necessary system calls, like stat() (which would probably require a new version) and the various libraries that depend on them (like glibc). These changes would require coordination of many projects. So even if ext4 developers implement initial support for creation-date timestamps, this feature will not be available to user programs for now. Which end up with the Linus final quote

Let's wait five years and see if there is actually any consensus on it being needed and used at all, rather than rush into something just because "we can".

xstat() {
  for target in "${@}"; do
    inode=$(ls -di "${target}" | cut -d ' ' -f 1)
    fs=$(df "${target}"  | tail -1 | awk '{print $1}')
    crtime=$(sudo debugfs -R 'stat <'"${inode}"'>' "${fs}" 2>/dev/null | 
    grep -oP 'crtime.*--\s*\K.*')
    printf "%s\t%s\n" "${crtime}" "${target}"
  done
}

运行它将返回创建日期:

:~$ echo 'print("hello world")' > blah.py
:~$ xstat "blah.py"
Mon Jul  6 13:43:39 2015    blah.py
:~$ echo 'print("goodbye world")' > blah.py
:~$ xstat "blah.py"
Mon Jul  6 13:43:39 2015    blah.py

因此,除非文件系统支持它,否则这是不可能的,如果文件系统支持,那么您可以使用子进程运行debugfs,并解析输出。

相关问题 更多 >