4

I want to get last 1 hours modified/created file with python. I tried this code but it's only getting the last file which created. If I created 10 files it shows only last one. How to get last 1 hours created file?

import glob
import os

list_of_files = glob.glob('c://*')
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)
U12-Forward
  • 65,118
  • 12
  • 70
  • 89
  • The `max()` function returns a single value. That is what it is supposed to do. If you want several files then your code needs to examine the value returned by `os.path.getctime()` for each file to see if it meets your criteria. So you need a loop that begins `for filename in list_of_files:`. – BoarGules Sep 26 '21 at 10:18
  • Can you show me with code ? how to do ? –  Sep 26 '21 at 10:21
  • 1
    You will need to **sort** the list of files using the same key, and then remove all those file in it that are outside the time interval (i.e. the last hour). – martineau Sep 26 '21 at 10:22

1 Answers1

2

You can do basically this:

  • get the list of files
  • get the time for each of them (also check os.path.getmtime() for updates)
  • use datetime module to get a value to compare against (that 1h)
  • compare

For that I've used a dictionary to both store paths and timestamps in a compact format. Then you can sort the dictionary by its values (dict.values()) (which is a float, timestamp) and by that you will get the latest files created within 1 hour that are sorted. (e.g. by sorted(...) function):

import os
import glob
from datetime import datetime, timedelta

hour_files = {
    key: val for key, val in {
        path: os.path.getctime(path)
        for path in glob.glob("./*")
    }.items()
    if datetime.fromtimestamp(val) >= datetime.now() - timedelta(hours=1)
}

Alternatively, without the comprehension:

files = glob.glob("./*")
times = {}
for path in files:
    times[path] = os.path.getctime(path)

hour_files = {}
for key, val in times.items():
    if datetime.fromtimestamp(val) < datetime.now() - timedelta(hours=1):
        continue
    hour_files[key] = val

Or, perhaps your folder is just a mess and you have too many files. In that case, approach it incrementally:

hour_files = {}
for file in glob.glob("./*"):
    timestamp = os.path.getctime(file)
    if datetime.fromtimestamp(timestamp) < datetime.now() - timedelta(hours=1):
        continue
    hour_files[file] = timestamp
Peter Badida
  • 10,502
  • 9
  • 40
  • 84
  • and what print to get result ? –  Sep 26 '21 at 10:45
  • `print(hour_files)` with the mapping, `print(hour_files.keys())` or `print(list(hour_files))` for just paths. After the [`sorted()` function](https://stackoverflow.com/a/613218/5994041) call, if you want to sort it, of course. – Peter Badida Sep 26 '21 at 10:46