1

I am new to python and I want to find all files in a directory that have been created within the past 24 hours. How do I filter the files that were created in 24 hour window.

This code will be used in Python 2.7 on Windows computer.

Chris
  • 21
  • 1
  • 6

1 Answers1

2

Get the stat of the file then check if its less then 24 hours... You will need to do loop/recreation...

import os
import time
st = os.stat("test.py")
ctime = st.st_ctime
print time.time() - ctime/3600 // hours
    if mtime<24:
       print mtime
BananaBuisness
  • 339
  • 2
  • 18
  • Note that this will only work on Windows. On Linux, `ctime` gives you the time of the most recent metadata change. [Docs](https://docs.python.org/2/library/os.html). – pushkin Nov 28 '15 at 23:06
  • Just google file creations date Linux Python – BananaBuisness Nov 28 '15 at 23:46
  • It clearly says in the docs that `st_ctime` does not refer to the creation time on Linux systems. If you google it, you will find [this](http://stackoverflow.com/questions/1408272/get-file-creation-time-with-python-on-linux) which confirms it. – pushkin Nov 29 '15 at 00:24
  • There is however a `st_birthtime` attribute but it's not available on all platforms. – pushkin Nov 29 '15 at 00:26
  • Thanks, the solution works great! – Chris Nov 30 '15 at 21:55
  • @Chris mark as answered please! (and you can up vote if you want to be really nice...) – BananaBuisness Nov 30 '15 at 23:34