How to get files modified in the last 5 minutes

You need to go through the current directory and get the files modified in the last 5 minutes. I am just starting this, and this is what I still

#!/usr/bin/python

import os,sys,time

dir = os.getcwd() 

print dir


for f in os.walk(dir):
    for i in os.stat(f).st_mtime:
    print i

when i run this i get this error

for i in os.stat(f).st_mtime:

TypeError: forced to Unicode: need a string or buffer, found a tuple

I would like to understand what causes this before moving on.

+4
source share
2 answers

You need to unzip, attach the file to the root directory and compare:

import os, time

_dir = os.getcwd()
files = (fle for rt, _, f in os.walk(_dir) for fle in f if time.time() - os.stat(
    os.path.join(rt, fle)).st_mtime < 300)

print(list(files))

os.stat(filename).st_mtimereturns a time that cannot be repeated, you need to compare this time with the current time, time.time()it returns seconds from an era, so you need to compare the difference between time.time() - os.stat(os.path.join(rt, fle)).st_mtimeand the number of minutes in seconds, i.e. 300 in your case.

, watchdog, , :

import sys
import time
import logging
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler

if __name__ == "__main__":
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = LoggingEventHandler()
    observer = Observer()
    observer.schedule(event_handler, path, recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()

.

+3

os.walk() , . - :

for root, dirs, files in walk(wav_root):
    for f in files:
        filename = root + f
        # Now use filename to call stat().st_mtime

. os.walk(), root f IIRC.

. : http://www.tutorialspoint.com/python/os_walk.htm

+6

Source: https://habr.com/ru/post/1615432/


All Articles