- 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的設計哲學:優雅,明確,簡單。提倡用一種方法,最好是只有一種方法來做一件事。
代碼的書寫要遵守規范,這樣有助於溝通和理解。
每種語言都有獨特的思想,初學者需要轉變思維、踏實踐行、堅持積累。