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

?

  • python : 3.7.0
  • OS : Ubuntu 18.04.1 LTS
  • IDE : PyCharm 2018.2.4
  • conda : 4.5.11
  • type setting : Markdown

?

code

"""
@Author : 行初心
@Date   : 18-10-2
@Blog   : www.cnblogs.com/xingchuxin
@GitHub : github.com/GratefulHeartCoder
"""
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中验证

[email protected]:~/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
创建时间:-
[email protected]:~/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] https://blog.csdn.net/qq_31828515/article/details/62886112
  • [2] https://blog.csdn.net/k346k346/article/details/78668100
  • [3] https://blog.lilydjwg.me/2018/7/11/get-file-birth-time-in-linux.213101.html

?

resource

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

?



Python具有开源、跨平台、解释型、交互式等特性,值得学习。

Python的设计哲学:优雅,明确,简单。提倡用一种方法,最好是只有一种方法来做一件事。

代码的书写要遵守规范,这样有助于沟通和理解。

每种语言都有独特的思想,初学者需要转变思维、踏实践行、坚持积累。

原文地址:https://www.cnblogs.com/xingchuxin/p/9737233.html

时间: 2024-08-30 05:32:04

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

Python3基础 open 打开txt文件 print(read) 其全文

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: # 输入 # 处理 # 输出 #file 文件类型的对象 file=open(r'F:\PersonKey.txt') print(type(file)) print(file) #读文本的全文并打印出来 print(file.read()) #这个时候再读的话,就返回一个EO

Python3基础 file with 配合文件操作

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code """ @Author : 行初心 @Date : 18-10-2 @Blog : www.cnblogs.com/xingchuxin @GitHub : github.com/GratefulHeartCoder """ de

Python3基础 hasattr 测试一个对象是否有指定的属性

镇场诗: 诚听如来语,顿舍世间名与利.愿做地藏徒,广演是经阎浮提. 愿尽吾所学,成就一良心博客.愿诸后来人,重现智慧清净体.------------------------------------------ code: class A : #属性 num=0 a=A() #属性的名字要用''包围起来 print(hasattr(a,'num')) print(hasattr(a,'n')) #否则 print(hasattr(a,num)) result: ============= REST

Python3基础 __len__,__getitem__ 记录列表中元素访问的次数 定制不可变序列,下标字典

? python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 conda : 4.5.11 type setting : Markdown ? code """ @Author : 行初心 @Date : 18-9-23 @Blog : www.cnblogs.com/xingchuxin @GitHub : github.com/GratefulHeartCoder """ #

Python3 基础 —— 模块 Module 介绍

1.模块的作用 在交互模式下输出的变量和函数定义,一旦终端重启后,这些定义就都不存在了,为了持久保存这些变量.函数等的定义,Python中引入了模块(Module)的概念.一个Python模块其实就是一个脚本文件,具有后缀".py",例如 hello.py 就是一个模块文件名,和普通文件一样可以被永久保存在本地存储磁盘中. 2.模块的内容 Python模块中存放的是一些程序代码,例如,变量定义.函数定义或是代码语句.下面是hello.py模块的内容,其中有一个变量 a,一个函数 fun

Python基础--Python3基础语法

Python3 基础语法 编码 默认情况下,Python3源码文件以UTF-8编码,所有字符串都是Unicode字符串.当然也可以为源码文件指定不同的编码,例如: # -*- coding: cp-1252 -*- 标识符 1.第一个字符必须是字母表中字母或下划线: 2.标识符的其他的部分有字母.数字和下划线组成: 3.标识符对大小写敏感. 注:在Python3中,非ASCII标识符也是允许的. Python保留字 保留字即关键字,我们不能把它们用作任何标识符名称.Python的标准库提供了一个

01月25日【Python3 基础知识】

01月25日[Python3 基础知识] 4.1 读写文件 4.2 文件方法 4.3 python2的乱码问题 4.4 python对passwd文件进行排序 4.1 读写文件 访问 模式 说 明 r 以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w 打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. a 打开一个文件用于追加.如果该文件已存在,文件指针将会放在文件的结尾.也就是说,新的内容将会被写入到已有内容之后.如果该文件不存在,创建新文件

Python基础(8)--文件

文件可以通过调用open或file来打开,open通常比file更通用,因为file几乎都是为面向对象程序设计量身打造 本文地址:http://www.cnblogs.com/archimedes/p/python-file.html,转载请注明源地址. 打开文件 打开文件程序会调用内置的open函数,首先是外部名,接着就是处理模式. 常见的文件运算: 在任何情况下,Python程序中的文本文件采用字符串的形式,读取文本时会返回字符串形式的文本 从文件中读取的数据回到脚本时是一个字符串,所以如果

python3 简单实现从csv文件中读取内容,并对内容进行分类统计

新手python刚刚上路,在实际工作中遇到如题所示的问题,尝试使用python3简单实现如下,欢迎高手前来优化 import csv #打开文件,用with打开可以不用去特意关闭file了,python3不支持file()打开文件,只能用open() with open("dk0519_1.csv","r",encoding="utf-8") as csv_file: #读取csv文件,返回的是迭代类型 read = csv.reader(csv