Hey guys, thanks a lot for the help.

Here's the code I have for going through each file in a directory I specify:

for root, dirs, files in os.walk('%s'%(DOSEnvironVar)):
    for this_file in files:
        if this_file.find('.csv')>-1:
            filepath = os.path.join(root, this_file)
            currentfile=open(filepath,'r')
            filelines = currentfile.readlines()

I would like to add a way to if the file was created (or modified) in the past three hours. So a change like this (pseudo code):

if this_file.find('.csv')>-1 and this_file was created in the past three hours:
   filepath = os.path.join(root, this_file)

I'm not very good with the time module yet, which is why I'm asking for help. Thanks!

Dani AI

Generated

A compact, practical note that builds on this thread: avoid opening every file up front (the original loop reads each file immediately). Test the file timestamp first and only open files that pass the age check. ’s idea of comparing epoch seconds is simple and effective, but there is an important platform caveat: on Windows ctime is the creation time, while on most Unix-like systems ctime is the inode/metadata-change time (not creation). If the intent is “last modified,” use the file modification time instead.

A clear, cross-platform-friendly pattern is to use pathlib plus datetime and compare a timezone-aware cutoff. This checks modification time and skips any I/O until the file is selected:

from pathlib import Path
from datetime import datetime, timedelta, timezone

cutoff = datetime.now(timezone.utc) - timedelta(hours=3)
for p in Path(DOSEnvironVar).rglob("*.csv"):
    mtime = datetime.fromtimestamp(p.stat().st_mtime, tz=timezone.utc)
    if mtime >= cutoff:
        # process the file (open/read) here

Additional tips: match extensions case-insensitively (*.CSV variants) or check p.suffix.lower(). For very large trees prefer os.scandir() or Path().rglob() to reduce overhead. If true file creation time is required on Unix, platform-specific APIs or a separate metadata store are needed because POSIX does not guarantee a portable creation timestamp.

The following function will return true if the given file is <= numsecs old. Just convert 3 hours to seconds and pass as an argument.

import os
import time

def newFile(file,numsecs):
	if time.time() - os.path.getctime(file) <= numsecs:
		return True
	else:
		return False
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.