Start New Discussion within our Software Development Community

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!

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
This question has already been answered. Start a new discussion instead.