4

I want to get all files modified/created in the last 1 hour with Python. I tried this code but it's only getting the last file which was created:

import glob
import os

list_of_files = glob.glob('c://*')
latest_file = max(list_of_files, key=os.path.getctime)
print(latest_file)

If I created 10 files it shows only last one. How to get all files created in the last 1 hour?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • 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

2 Answers2

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
  • 11,310
  • 10
  • 44
  • 90
  • 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
-1

Here is another solution, shorter, that uses only the os package:

import os
directory = "/path/to/directory"
latest_file = os.popen(f"ls -t {directory}").read().split("\n")[0]
Bouliech
  • 84
  • 1
  • 3
  • 1
    The OP already has a solution for the last file. They are asking for all files changed in the last hour – Tomerikoo Feb 15 '23 at 15:10