Cut files into another folder, if they aren't open for a certain period of time - python script

I have one script that is generating a bunch of files in one folder. As the pile of files is increasing, my script is becoming slower. I would like to create a python script that will look at the folder and every file that is created and not opened for half an hour (I need this condition as my script is reading them, after it creates) to be moved (cut) with some other folder. I am familiar with pandas and numpy, but not with this area.. Can someone help me? Found this answer, however, it didn't help me to achieve what I want.


Solution 1:

Depending on the required file timestamp, e.g. see:

How to get the first accessed time file was created along with the last modified time?

As a rough example:

  • print all files in temp-folder (based on OS's environment variable) along with their last-modified timestamp
import os

def list_files(folder):
    # your attempt
    return os.listdir(folder)


folder = os.getenv('TEMP')
for filename in list_files(folder):
    last_modified = os.path.getmtime(filename)
    print(f"{filename} last modified: {last_modified}")

Prints some lines like those:

.Xdefaults last modified: 1531860388.7936974

.gnome last modified: 1588427978.179283