Python Forum
Have to use Python 2.x to find "yesterday' files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Have to use Python 2.x to find "yesterday' files
#3
from __future__ import print_function

from collections import namedtuple
from datetime import datetime, timedelta
from os import listdir
from os.path import getmtime
from os.path import join as joinpath

Result = namedtuple("File", "file age")


def calculate_days(td):
    # you can also round here or convert it to an int
    return td.total_seconds() / 3600 / 24


def get_mtime(file):
    return datetime.fromtimestamp(getmtime(file))


def find(root_path, file_pattern, min_age_days=1):
    min_age = timedelta(days=min_age_days)
    now = datetime.now()

    for file in listdir(root_path):
        if file_pattern in file:
            file = joinpath(root_path, file)
            mtime = get_mtime(file)
            delta = now - mtime
            if delta > min_age:
                # if unwanted, you can refactor this and remove the age from the Result
                # or just yield the file, which is a str
                yield Result(file, calculate_days(delta))


if __name__ == "__main__":
    for file in find("/home/deadeye/", ".py"):
        print(file.file) # accessing the Result, which is a namedtuple
        # print(file) will print the representation of the namedtuple with data
        # print(file.age) will show the age in days
        # but you can also use index access on a Result
        # print(file[0]) will print the first field, which is file

    # a namedtuple act as a tuple and supports argument unpacking
    for file, age in find("/home/deadeye/", ".py"):
        pass
        # file is a file
        # age is the age of the file


    # or to get a list:
    # files = list(find("/home/deadeye", ".py"))
    # print(files)
Works with Python 2.7 and Python 3.x+

By the way, the order of files in a directory is defined by their inode number and not the creation date.
Some scientists expected that os.path.listdir() return the alphabetical order, which is not right.

If the alphabetical order is required, you can wrap sorted around listdir.

Before:
for file in listdir(root_path):
After:
for file in sorted(listdir(root_path)):
Don't forget, that listdir is from os imported.
You can import functions, classes and names form modules with:
from os import listdir
Then you get the name listdir on module level, which is from os.

If you have Chinese characters in your file names, good luck.
The Unicode support is not good with Python 2.7

So if you require an Argument why not to use Python 2.7:
It is dangerous, because there are no more security patches.

If you're locked in an Env without internet access, this is ok.
If the device has internet access, Python 2.7 should not be used because of the huge security risks.

You can look, which Security Patches Python 3.x got since Python 2.7 were stopped with development.
It's a long list and very often the parsers do have some issues.
Last thing i saw, was an change of the module urllib.parse. Python 2.7 does not get this important updates.
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Have to use Python 2.x to find "yesterday' files - by DeaD_EyE - Sep-18-2021, 01:36 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Find duplicate files in multiple directories Pavel_47 9 3,144 Dec-27-2022, 04:47 PM
Last Post: deanhystad
  count every 28 files and find desire files RolanRoll 3 2,085 Dec-12-2021, 04:42 PM
Last Post: Axel_Erfurt
  find 'yesterdays files' tester_V 8 3,872 Jun-18-2021, 02:10 AM
Last Post: tester_V
  Find and replace in files with regex and Python Melcu54 0 1,853 Jun-03-2021, 09:33 AM
Last Post: Melcu54
  List of error codes to find (and count) in all files in a directory tester_V 8 3,718 Dec-11-2020, 07:07 PM
Last Post: tester_V
  helping PyInstaller To Find files Harshil 0 1,491 Aug-30-2020, 10:16 AM
Last Post: Harshil
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 3,614 Aug-25-2020, 01:52 AM
Last Post: tester_V
  python 3 find difference between 2 files pd007 2 2,145 May-22-2020, 01:16 AM
Last Post: Larz60+
  Find all “*.wav” files that created yesterday on linux host with python. pydeev 6 4,799 Jan-07-2020, 06:43 AM
Last Post: pydeev
  finding yesterday and tomorrrow without using date.time module apexman 10 5,534 Feb-25-2019, 05:33 AM
Last Post: samsonite

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020