/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

  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 1,
  6. "metadata": {},
  7. "outputs": [],
  8. "source": [
  9. "import os\n",
  10. "import pathlib\n",
  11. "import datetime\n",
  12. "import time\n",
  13. "import platform"
  14. ]
  15. },
  16. {
  17. "cell_type": "code",
  18. "execution_count": 2,
  19. "metadata": {},
  20. "outputs": [],
  21. "source": [
  22. "p = pathlib.Path('data/temp/test.txt')"
  23. ]
  24. },
  25. {
  26. "cell_type": "code",
  27. "execution_count": 3,
  28. "metadata": {},
  29. "outputs": [
  30. {
  31. "data": {
  32. "text/plain": [
  33. "6"
  34. ]
  35. },
  36. "execution_count": 3,
  37. "metadata": {},
  38. "output_type": "execute_result"
  39. }
  40. ],
  41. "source": [
  42. "p.write_text('test')\n",
  43. "\n",
  44. "time.sleep(10)\n",
  45. "\n",
  46. "p.write_text('update')"
  47. ]
  48. },
  49. {
  50. "cell_type": "code",
  51. "execution_count": 4,
  52. "metadata": {},
  53. "outputs": [
  54. {
  55. "name": "stdout",
  56. "output_type": "stream",
  57. "text": [
  58. "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"
  59. ]
  60. }
  61. ],
  62. "source": [
  63. "print(p.stat())"
  64. ]
  65. },
  66. {
  67. "cell_type": "code",
  68. "execution_count": 5,
  69. "metadata": {},
  70. "outputs": [
  71. {
  72. "name": "stdout",
  73. "output_type": "stream",
  74. "text": [
  75. "<class 'os.stat_result'>\n"
  76. ]
  77. }
  78. ],
  79. "source": [
  80. "print(type(p.stat()))"
  81. ]
  82. },
  83. {
  84. "cell_type": "code",
  85. "execution_count": 6,
  86. "metadata": {},
  87. "outputs": [
  88. {
  89. "name": "stdout",
  90. "output_type": "stream",
  91. "text": [
  92. "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"
  93. ]
  94. }
  95. ],
  96. "source": [
  97. "print(os.stat('data/temp/test.txt'))"
  98. ]
  99. },
  100. {
  101. "cell_type": "code",
  102. "execution_count": 7,
  103. "metadata": {},
  104. "outputs": [
  105. {
  106. "name": "stdout",
  107. "output_type": "stream",
  108. "text": [
  109. "<class 'os.stat_result'>\n"
  110. ]
  111. }
  112. ],
  113. "source": [
  114. "print(type(os.stat('data/temp/test.txt')))"
  115. ]
  116. },
  117. {
  118. "cell_type": "code",
  119. "execution_count": 8,
  120. "metadata": {},
  121. "outputs": [
  122. {
  123. "name": "stdout",
  124. "output_type": "stream",
  125. "text": [
  126. "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"
  127. ]
  128. }
  129. ],
  130. "source": [
  131. "print(os.stat(p))"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 9,
  137. "metadata": {},
  138. "outputs": [
  139. {
  140. "name": "stdout",
  141. "output_type": "stream",
  142. "text": [
  143. "<class 'os.stat_result'>\n"
  144. ]
  145. }
  146. ],
  147. "source": [
  148. "print(type(os.stat(p)))"
  149. ]
  150. },
  151. {
  152. "cell_type": "code",
  153. "execution_count": 10,
  154. "metadata": {},
  155. "outputs": [
  156. {
  157. "name": "stdout",
  158. "output_type": "stream",
  159. "text": [
  160. "True\n"
  161. ]
  162. }
  163. ],
  164. "source": [
  165. "print(p.stat() == os.stat('data/temp/test.txt') == os.stat(p))"
  166. ]
  167. },
  168. {
  169. "cell_type": "code",
  170. "execution_count": 11,
  171. "metadata": {},
  172. "outputs": [],
  173. "source": [
  174. "st = p.stat()"
  175. ]
  176. },
  177. {
  178. "cell_type": "code",
  179. "execution_count": 12,
  180. "metadata": {},
  181. "outputs": [
  182. {
  183. "name": "stdout",
  184. "output_type": "stream",
  185. "text": [
  186. "1549094615.972488\n"
  187. ]
  188. }
  189. ],
  190. "source": [
  191. "print(st.st_atime)"
  192. ]
  193. },
  194. {
  195. "cell_type": "code",
  196. "execution_count": 13,
  197. "metadata": {},
  198. "outputs": [
  199. {
  200. "name": "stdout",
  201. "output_type": "stream",
  202. "text": [
  203. "1549094615.9723485\n"
  204. ]
  205. }
  206. ],
  207. "source": [
  208. "print(st.st_mtime)"
  209. ]
  210. },
  211. {
  212. "cell_type": "code",
  213. "execution_count": 14,
  214. "metadata": {},
  215. "outputs": [
  216. {
  217. "name": "stdout",
  218. "output_type": "stream",
  219. "text": [
  220. "1549094615.9723485\n"
  221. ]
  222. }
  223. ],
  224. "source": [
  225. "print(st.st_ctime)"
  226. ]
  227. },
  228. {
  229. "cell_type": "code",
  230. "execution_count": 15,
  231. "metadata": {},
  232. "outputs": [
  233. {
  234. "name": "stdout",
  235. "output_type": "stream",
  236. "text": [
  237. "1549094605.9650702\n"
  238. ]
  239. }
  240. ],
  241. "source": [
  242. "print(st.st_birthtime)"
  243. ]
  244. },
  245. {
  246. "cell_type": "code",
  247. "execution_count": 16,
  248. "metadata": {},
  249. "outputs": [
  250. {
  251. "name": "stdout",
  252. "output_type": "stream",
  253. "text": [
  254. "<class 'float'>\n"
  255. ]
  256. }
  257. ],
  258. "source": [
  259. "print(type(st.st_ctime))"
  260. ]
  261. },
  262. {
  263. "cell_type": "code",
  264. "execution_count": 17,
  265. "metadata": {},
  266. "outputs": [
  267. {
  268. "name": "stdout",
  269. "output_type": "stream",
  270. "text": [
  271. "1549094615972348510\n"
  272. ]
  273. }
  274. ],
  275. "source": [
  276. "print(st.st_ctime_ns)"
  277. ]
  278. },
  279. {
  280. "cell_type": "code",
  281. "execution_count": 18,
  282. "metadata": {},
  283. "outputs": [
  284. {
  285. "name": "stdout",
  286. "output_type": "stream",
  287. "text": [
  288. "<class 'int'>\n"
  289. ]
  290. }
  291. ],
  292. "source": [
  293. "print(type(st.st_ctime_ns))"
  294. ]
  295. },
  296. {
  297. "cell_type": "code",
  298. "execution_count": 19,
  299. "metadata": {},
  300. "outputs": [
  301. {
  302. "name": "stdout",
  303. "output_type": "stream",
  304. "text": [
  305. "1549094615.972488\n"
  306. ]
  307. }
  308. ],
  309. "source": [
  310. "print(os.path.getatime('data/temp/test.txt'))"
  311. ]
  312. },
  313. {
  314. "cell_type": "code",
  315. "execution_count": 20,
  316. "metadata": {},
  317. "outputs": [
  318. {
  319. "name": "stdout",
  320. "output_type": "stream",
  321. "text": [
  322. "1549094615.9723485\n"
  323. ]
  324. }
  325. ],
  326. "source": [
  327. "print(os.path.getmtime('data/temp/test.txt'))"
  328. ]
  329. },
  330. {
  331. "cell_type": "code",
  332. "execution_count": 21,
  333. "metadata": {},
  334. "outputs": [
  335. {
  336. "name": "stdout",
  337. "output_type": "stream",
  338. "text": [
  339. "1549094615.9723485\n"
  340. ]
  341. }
  342. ],
  343. "source": [
  344. "print(os.path.getctime('data/temp/test.txt'))"
  345. ]
  346. },
  347. {
  348. "cell_type": "code",
  349. "execution_count": 22,
  350. "metadata": {},
  351. "outputs": [
  352. {
  353. "name": "stdout",
  354. "output_type": "stream",
  355. "text": [
  356. "1549094615.9723485\n"
  357. ]
  358. }
  359. ],
  360. "source": [
  361. "print(os.path.getctime(p))"
  362. ]
  363. },
  364. {
  365. "cell_type": "code",
  366. "execution_count": 23,
  367. "metadata": {},
  368. "outputs": [
  369. {
  370. "name": "stdout",
  371. "output_type": "stream",
  372. "text": [
  373. "True\n"
  374. ]
  375. }
  376. ],
  377. "source": [
  378. "print(os.path.getctime(p) == p.stat().st_ctime)"
  379. ]
  380. },
  381. {
  382. "cell_type": "code",
  383. "execution_count": 24,
  384. "metadata": {},
  385. "outputs": [],
  386. "source": [
  387. "dt = datetime.datetime.fromtimestamp(p.stat().st_ctime)"
  388. ]
  389. },
  390. {
  391. "cell_type": "code",
  392. "execution_count": 25,
  393. "metadata": {},
  394. "outputs": [
  395. {
  396. "name": "stdout",
  397. "output_type": "stream",
  398. "text": [
  399. "2019-02-02 17:03:35.972348\n"
  400. ]
  401. }
  402. ],
  403. "source": [
  404. "print(dt)"
  405. ]
  406. },
  407. {
  408. "cell_type": "code",
  409. "execution_count": 26,
  410. "metadata": {},
  411. "outputs": [
  412. {
  413. "name": "stdout",
  414. "output_type": "stream",
  415. "text": [
  416. "<class 'datetime.datetime'>\n"
  417. ]
  418. }
  419. ],
  420. "source": [
  421. "print(type(dt))"
  422. ]
  423. },
  424. {
  425. "cell_type": "code",
  426. "execution_count": 27,
  427. "metadata": {},
  428. "outputs": [
  429. {
  430. "name": "stdout",
  431. "output_type": "stream",
  432. "text": [
  433. "2019年02月02日 17:03:35\n"
  434. ]
  435. }
  436. ],
  437. "source": [
  438. "print(dt.strftime('%Y年%m月%d日 %H:%M:%S'))"
  439. ]
  440. },
  441. {
  442. "cell_type": "code",
  443. "execution_count": 28,
  444. "metadata": {},
  445. "outputs": [
  446. {
  447. "name": "stdout",
  448. "output_type": "stream",
  449. "text": [
  450. "2019-02-02T17:03:35.972348\n"
  451. ]
  452. }
  453. ],
  454. "source": [
  455. "print(dt.isoformat())"
  456. ]
  457. },
  458. {
  459. "cell_type": "code",
  460. "execution_count": 29,
  461. "metadata": {},
  462. "outputs": [
  463. {
  464. "name": "stdout",
  465. "output_type": "stream",
  466. "text": [
  467. "1549094615.9723485\n"
  468. ]
  469. }
  470. ],
  471. "source": [
  472. "print(os.path.getmtime('data/temp/test.txt'))"
  473. ]
  474. },
  475. {
  476. "cell_type": "code",
  477. "execution_count": 30,
  478. "metadata": {},
  479. "outputs": [
  480. {
  481. "name": "stdout",
  482. "output_type": "stream",
  483. "text": [
  484. "1549094615.9723485\n"
  485. ]
  486. }
  487. ],
  488. "source": [
  489. "print(p.stat().st_mtime)"
  490. ]
  491. },
  492. {
  493. "cell_type": "code",
  494. "execution_count": 31,
  495. "metadata": {},
  496. "outputs": [
  497. {
  498. "name": "stdout",
  499. "output_type": "stream",
  500. "text": [
  501. "2019-02-02 17:03:35.972348\n"
  502. ]
  503. }
  504. ],
  505. "source": [
  506. "print(datetime.datetime.fromtimestamp(p.stat().st_mtime))"
  507. ]
  508. },
  509. {
  510. "cell_type": "code",
  511. "execution_count": 32,
  512. "metadata": {},
  513. "outputs": [],
  514. "source": [
  515. "def creation_date(path_to_file):\n",
  516. " \"\"\"\n",
  517. " Try to get the date that a file was created, falling back to when it was\n",
  518. " last modified if that isn't possible.\n",
  519. " See http://stackoverflow.com/a/39501288/1709587 for explanation.\n",
  520. " \"\"\"\n",
  521. " if platform.system() == 'Windows':\n",
  522. " return os.path.getctime(path_to_file)\n",
  523. " else:\n",
  524. " stat = os.stat(path_to_file)\n",
  525. " try:\n",
  526. " return stat.st_birthtime\n",
  527. " except AttributeError:\n",
  528. " # We're probably on Linux. No easy way to get creation dates here,\n",
  529. " # so we'll settle for when its content was last modified.\n",
  530. " return stat.st_mtime"
  531. ]
  532. },
  533. {
  534. "cell_type": "code",
  535. "execution_count": 33,
  536. "metadata": {},
  537. "outputs": [
  538. {
  539. "name": "stdout",
  540. "output_type": "stream",
  541. "text": [
  542. "1549094605.9650702\n"
  543. ]
  544. }
  545. ],
  546. "source": [
  547. "print(creation_date(p))"
  548. ]
  549. },
  550. {
  551. "cell_type": "code",
  552. "execution_count": 34,
  553. "metadata": {},
  554. "outputs": [
  555. {
  556. "name": "stdout",
  557. "output_type": "stream",
  558. "text": [
  559. "2019-02-02 17:03:25.965070\n"
  560. ]
  561. }
  562. ],
  563. "source": [
  564. "print(datetime.datetime.fromtimestamp(creation_date(p)))"
  565. ]
  566. }
  567. ],
  568. "metadata": {
  569. "kernelspec": {
  570. "display_name": "Python 3",
  571. "language": "python",
  572. "name": "python3"
  573. },
  574. "language_info": {
  575. "codemirror_mode": {
  576. "name": "ipython",
  577. "version": 3
  578. },
  579. "file_extension": ".py",
  580. "mimetype": "text/x-python",
  581. "name": "python",
  582. "nbconvert_exporter": "python",
  583. "pygments_lexer": "ipython3",
  584. "version": "3.7.2"
  585. }
  586. },
  587. "nbformat": 4,
  588. "nbformat_minor": 2
  589. }