• Python3基础 getatime getctime getmtime 文件的最近访问 + 属性修改 + 内容修改时间


    •        Python : 3.7.0
    •          OS : Ubuntu 18.04.1 LTS
    •         IDE : PyCharm 2018.2.4
    •       Conda : 4.5.11
    •    typesetting : Markdown

    code

    """
    @Author : 行初心
    @Date   : 18-10-2
    @Blog   : www.cnblogs.com/xingchuxin
    @Gitee  : gitee.com/zhichengjiu
    """
    import time
    import os
    
    
    def main():
        file_name = '1.txt'
    
        # 文件的最近访问时间
        file_times_access = time.localtime(os.path.getatime(file_name))
        year_access = file_times_access.tm_year
        month_access = file_times_access.tm_mon
        day_access = file_times_access.tm_mday
    
        hour_access = file_times_access.tm_hour
        minute_access = file_times_access.tm_min
        second_access = file_times_access.tm_sec
    
        print('文件的最近访问时间(atime):  ', year_access, '年', month_access, '月', day_access, '日', '  ', hour_access, '时',
              minute_access, '分', second_access, '秒')
    
        # 文件属性最近修改的时间
        file_times_create = time.localtime(os.path.getctime(file_name))
        year_create = file_times_create.tm_year
        month_create = file_times_create.tm_mon
        day_create = file_times_create.tm_mday
    
        hour_create = file_times_create.tm_hour
        minute_create = file_times_create.tm_min
        second_create = file_times_create.tm_sec
        print('文件属性最近修改的时间(ctime):  ', year_create, '年', month_create, '月', day_create, '日', '  ', hour_create, '时', minute_create,
              '分', second_create, '秒')
    
        # 文件的内容最近修改的时间
        file_times_modified = time.localtime(os.path.getmtime(file_name))
        year_modified = file_times_modified.tm_year
        month_modified = file_times_modified.tm_mon
        day_modified = file_times_modified.tm_mday
    
        hour_modified = file_times_modified.tm_hour
        minute_modified = file_times_modified.tm_min
        second_modified = file_times_modified.tm_sec
        print('文件的内容最近修改的时间(mtime):  ', year_modified, '年', month_modified, '月', day_modified, '日', '  ', hour_modified, '时',
              minute_modified, '分', second_modified, '秒')
    
    
    if __name__ == '__main__':
        main()
    
    

    result

    /home/coder/anaconda3/envs/py37/bin/python /home/coder/PycharmProjects/base/demo.py
    文件的最近访问时间(atime):   2018 年 10 月 2 日    12 时 25 分 26 秒
    文件属性最近修改的时间(ctime):   2018 年 10 月 2 日    12 时 33 分 13 秒
    文件的内容最近修改的时间(mtime):   2018 年 10 月 2 日    12 时 25 分 8 秒
    
    Process finished with exit code 0
    
    

    在terminal中验证

    coder@Ubuntu:~/PycharmProjects/base$ stat 1.txt 
      文件:1.txt
      大小:13        	块:8          IO 块:4096   普通文件
    设备:808h/2056d	Inode:529035      硬链接:1
    权限:(0777/-rwxrwxrwx)  Uid:( 1000/   coder)   Gid:( 1000/   coder)
    最近访问:2018-10-02 12:25:26.445044634 +0800
    最近更改:2018-10-02 12:25:08.688137355 +0800
    最近改动:2018-10-02 12:33:13.972664234 +0800
    创建时间:-
    coder@Ubuntu:~/PycharmProjects/base$ 
    
    

    more knowledge

    • linux中,文件的三个时间分别是:Access、Modify和Change[1]。(注意:没有Create)
    • ext4文件系统中有文件创建时间,其字段为crtime[2]。(但是,使用stat命令后发现 -> 创建时间:-)
    • 内核已经通过 4.11 版本引入的 statx 系统调用支持获取创建时间了。[3]
      在内核源码树中有现成的 samples/statx/test-statx.c[3]
      编译:gcc -O2 -o statx test-statx.c[3]

    reference

    • [1] blog.csdn.net/qq_31828515/article/details/62886112
    • [2] blog.csdn.net/k346k346/article/details/78668100
    • [3] blog.lilydjwg.me/2018/7/11/get-file-birth-time-in-linux.213101.html

    resource

    • [文档] docs.python.org/3
    • [规范] www.python.org/dev/peps/pep-0008
    • [规范] zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_language_rules
    • [源码] www.python.org/downloads/source
    • [ PEP ] www.python.org/dev/peps
    • [平台] www.cnblogs.com
    • [平台] gitee.com


    Python具有开源、跨平台、解释型、交互式等特性,值得学习。
    Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。
    代码的书写要遵守规范,这样有助于沟通和理解。
    每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

  • 相关阅读:
    Windows服务BAT命令-安装、卸载、启动、停止
    身份认证
    密钥管理概述
    快速上手几个Linux命令
    递归
    数字签名的实现方案
    数字签名
    密码学基础
    你可以把Linux内核当成一家软件外包公司的老板
    数学归纳法
  • 原文地址:https://www.cnblogs.com/xingchuxin/p/10426303.html
Copyright © 2020-2023  润新知