Created
June 7, 2020 11:59
-
-
Save nikhilkumarsingh/1b2510fccceca52c16f6a52ebfab6793 to your computer and use it in GitHub Desktop.
Fetching File Timestamps using Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Fetching File Timestamps using Python" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from pathlib import Path" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"sample\ttest.py Tutorial.ipynb\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!ls" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"p = Path(\"test.py\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"os.stat_result(st_mode=33204, st_ino=4981505, st_dev=2052, st_nlink=1, st_uid=1000, st_gid=1000, st_size=80, st_atime=1591524289, st_mtime=1590327260, st_ctime=1590327261)" | |
] | |
}, | |
"execution_count": 5, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.stat()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"1590327260.9186883" | |
] | |
}, | |
"execution_count": 7, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"p.stat().st_mtime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"from datetime import datetime" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"'2020-05-24T19:04:20.918688'" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"datetime.fromtimestamp(p.stat().st_mtime).isoformat()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"p = Path(\"sample\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 19, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"abc.json a.txt b.txt\tc.txt foo.py\r\n" | |
] | |
} | |
], | |
"source": [ | |
"!ls sample" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"ok" | |
] | |
} | |
], | |
"source": [ | |
"!cat sample/b.txt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 18, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"[PosixPath('sample/c.txt'),\n", | |
" PosixPath('sample/a.txt'),\n", | |
" PosixPath('sample/abc.json'),\n", | |
" PosixPath('sample/foo.py'),\n", | |
" PosixPath('sample/b.txt')]" | |
] | |
}, | |
"execution_count": 18, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"sorted(p.iterdir(), key=lambda p: p.stat().st_atime)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Timestamps:\n", | |
"\n", | |
"#### st_atime\n", | |
"Time of most recent access expressed in seconds.\n", | |
"\n", | |
"#### st_mtime\n", | |
"Time of most recent content modification expressed in seconds.\n", | |
"\n", | |
"#### st_ctime\n", | |
"Platform dependent:\n", | |
"- the time of most recent metadata change on Unix,\n", | |
"- the time of creation on Windows, expressed in seconds.\n", | |
"\n", | |
"### References:\n", | |
"\n", | |
"- https://docs.python.org/3/library/pathlib.html#pathlib.Path.stat\n", | |
"- https://docs.python.org/3/library/os.html#os.stat\n", | |
"- https://renenyffenegger.ch/notes/Linux/filesystem/file-directory-metadata\n", | |
"- https://superuser.com/questions/898017/windows-command-to-get-all-information-properties-of-a-file" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "pyenv37", | |
"language": "python", | |
"name": "pyenv37" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.7.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment