/notebook/os_stat.ipynb
https://github.com/nkmk/python-snippets · Jupyter · 589 lines · 589 code · 0 blank · 0 comment · 0 complexity · eaddcfdfdcdc72f87d30fcdbd176b556 MD5 · raw file
- {
- "cells": [
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {},
- "outputs": [],
- "source": [
- "import os\n",
- "import pathlib\n",
- "import datetime\n",
- "import time\n",
- "import platform"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {},
- "outputs": [],
- "source": [
- "p = pathlib.Path('data/temp/test.txt')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {},
- "outputs": [
- {
- "data": {
- "text/plain": [
- "6"
- ]
- },
- "execution_count": 3,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "p.write_text('test')\n",
- "\n",
- "time.sleep(10)\n",
- "\n",
- "p.write_text('update')"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "os.stat_result(st_mode=33188, st_ino=8728494137, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=6, st_atime=1549094615, st_mtime=1549094615, st_ctime=1549094615)\n"
- ]
- }
- ],
- "source": [
- "print(p.stat())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'os.stat_result'>\n"
- ]
- }
- ],
- "source": [
- "print(type(p.stat()))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "os.stat_result(st_mode=33188, st_ino=8728494137, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=6, st_atime=1549094615, st_mtime=1549094615, st_ctime=1549094615)\n"
- ]
- }
- ],
- "source": [
- "print(os.stat('data/temp/test.txt'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'os.stat_result'>\n"
- ]
- }
- ],
- "source": [
- "print(type(os.stat('data/temp/test.txt')))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "os.stat_result(st_mode=33188, st_ino=8728494137, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=6, st_atime=1549094615, st_mtime=1549094615, st_ctime=1549094615)\n"
- ]
- }
- ],
- "source": [
- "print(os.stat(p))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'os.stat_result'>\n"
- ]
- }
- ],
- "source": [
- "print(type(os.stat(p)))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n"
- ]
- }
- ],
- "source": [
- "print(p.stat() == os.stat('data/temp/test.txt') == os.stat(p))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {},
- "outputs": [],
- "source": [
- "st = p.stat()"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.972488\n"
- ]
- }
- ],
- "source": [
- "print(st.st_atime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(st.st_mtime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(st.st_ctime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094605.9650702\n"
- ]
- }
- ],
- "source": [
- "print(st.st_birthtime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'float'>\n"
- ]
- }
- ],
- "source": [
- "print(type(st.st_ctime))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615972348510\n"
- ]
- }
- ],
- "source": [
- "print(st.st_ctime_ns)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 18,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'int'>\n"
- ]
- }
- ],
- "source": [
- "print(type(st.st_ctime_ns))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 19,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.972488\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getatime('data/temp/test.txt'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 20,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getmtime('data/temp/test.txt'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 21,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getctime('data/temp/test.txt'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 22,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getctime(p))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 23,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "True\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getctime(p) == p.stat().st_ctime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 24,
- "metadata": {},
- "outputs": [],
- "source": [
- "dt = datetime.datetime.fromtimestamp(p.stat().st_ctime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 25,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2019-02-02 17:03:35.972348\n"
- ]
- }
- ],
- "source": [
- "print(dt)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 26,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "<class 'datetime.datetime'>\n"
- ]
- }
- ],
- "source": [
- "print(type(dt))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 27,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2019年02月02日 17:03:35\n"
- ]
- }
- ],
- "source": [
- "print(dt.strftime('%Y年%m月%d日 %H:%M:%S'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 28,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2019-02-02T17:03:35.972348\n"
- ]
- }
- ],
- "source": [
- "print(dt.isoformat())"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 29,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(os.path.getmtime('data/temp/test.txt'))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 30,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094615.9723485\n"
- ]
- }
- ],
- "source": [
- "print(p.stat().st_mtime)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 31,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2019-02-02 17:03:35.972348\n"
- ]
- }
- ],
- "source": [
- "print(datetime.datetime.fromtimestamp(p.stat().st_mtime))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 32,
- "metadata": {},
- "outputs": [],
- "source": [
- "def creation_date(path_to_file):\n",
- " \"\"\"\n",
- " Try to get the date that a file was created, falling back to when it was\n",
- " last modified if that isn't possible.\n",
- " See http://stackoverflow.com/a/39501288/1709587 for explanation.\n",
- " \"\"\"\n",
- " if platform.system() == 'Windows':\n",
- " return os.path.getctime(path_to_file)\n",
- " else:\n",
- " stat = os.stat(path_to_file)\n",
- " try:\n",
- " return stat.st_birthtime\n",
- " except AttributeError:\n",
- " # We're probably on Linux. No easy way to get creation dates here,\n",
- " # so we'll settle for when its content was last modified.\n",
- " return stat.st_mtime"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 33,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "1549094605.9650702\n"
- ]
- }
- ],
- "source": [
- "print(creation_date(p))"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 34,
- "metadata": {},
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "2019-02-02 17:03:25.965070\n"
- ]
- }
- ],
- "source": [
- "print(datetime.datetime.fromtimestamp(creation_date(p)))"
- ]
- }
- ],
- "metadata": {
- "kernelspec": {
- "display_name": "Python 3",
- "language": "python",
- "name": "python3"
- },
- "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.2"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 2
- }