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
#1
Greetings!
I'm trying to identify (and later copy) some files, the problem is the systems I must use have only Python 2.7 installed.
I managed to get the timestamp of the files I'm scanning and the 'yesterday' string to compare the files against.
I'm still failing to identify the "yesterday' files.
here is the code:
import os
import datetime as dt
import datetime

prd_dr = 'c:\\02\\'

for items in os.listdir(prd_dr) :
    items=items.strip()
    if 'Monitor.' in items :        
        fls = os.path.join(prd_dr,items)
        print(' Path_file -> ',fls)
        tt = os.path.getmtime(fls)
        time_stamp=datetime.datetime.fromtimestamp(tt)     
        print(fls,time_stamp)

        yesterday = dt.datetime.now().date() - dt.timedelta(days=1)
        print ('YEStERDAY',yesterday)
        if time_stamp == yesterday :
            print('---------',fls)
Thank you!
Reply
#2
time_stamp will be a timestamp to the second. yesterday is only a date. Only at exact midnight would they be equal. You either want to compare to a range of timestamps or you want to force timestamp to a date.

timestamp_date = datetime.datetime.fromtimestamp(tt).date()

if timestamp_date == yesterday:
    ...
Reply
#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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
Dead_EyE, that is a great snippet you are sharing, thank you!
I think I will end up using it as is.

Still would like to fix my own code, I made changes to it...
it still does not work, for some reason, it fails to compare two date objects at the end of the code.

Could anyone correct it?
import datetime as dt
import os.path
import time,datetime

prd_dr = 'c:\\02\\'
for items in os.listdir(production_path) :
    items=items.strip()
    if 'Monitor.' in items :        
        fls = os.path.join(production_path,items)
        print('PATH -> ',type(fls))
		
        modif = os.path.getmtime(fls)
        xx =year,month,day,hour,minute,second=time.localtime(modif)[:-3]
        xx = ("%d-%02d-%02d"%(year,month,day))

        conv_dto = datetime.datetime.strptime(xx, '%Y-%m-%d')
        dat = conv_dto.date()
        print(type(dat))
        yesterday = dt.datetime.now().date() - dt.timedelta(days=1)
        print(type(yesterday))

        if yesterday == dat :
            print('File modified yesterday',fls)   # <--- Failing to compare two ojects, NO errors.
Thank you!
Reply
#5
My bad!
Sorry for the confusion!
The Second snipped I posted works fine, I just do not have a "yesterday' "file in the directory.

Sorry again!
I really appreciate your help and the coaching that is priceless...

Thank you!
Reply
#6
The name of your file is inconsistent between lines 5, 6 and 9. This version just dies with errors. I don't think this is a cut&paste of your working version.

Lines 13-17 look like a convoluted way to convert the timestamp into a date object. I think cleaner would be
dat = dt.datetime.fromtimestamp(modif).date()
If I fix the filename issue on line 5, it appears to work. Rather than printing the type your dates, maybe you should print the actual dates and see if they're what you're expecting. It properly finds files named Monitor.* that were modified yesterday.
Reply
#7
You are correct!
The file name inconsistency is just a typo...
I also do not have much skills in programming....

I thought I will get the real dates from all files that have 'Monitor.' in names by using 'getmtime',
then will format it to 'year,month,day' and convert it to dateobject.

        modif = os.path.getmtime(fls)
        xx =year,month,day,hour,minute,second=time.localtime(modif)[:-3]
        xx = ("%d-%02d-%02d"%(year,month,day))
       conv_dto = datetime.datetime.strptime(xx, '%Y-%m-%d')
I'm comparing dateobject 'Monitor.' files with the 'yesterday' dataobject to get the files that were last updated yesterday
        yesterday = dt.datetime.now().date() - dt.timedelta(days=1)
        print(type(yesterday))
 
        if yesterday == dat :
            print('File modified yesterday',fls)
I printed "type" of the dates just for a debug purpose to make sure I compare the same data types.
print(type(dat))
that was the problem in my first script.

I really appreciate your input!
Thank you! for the snippet and the coaching!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  find 'yesterdays files' tester_V 8 909 Jun-18-2021, 02:10 AM
Last Post: tester_V
  Find and replace in files with regex and Python Melcu54 0 597 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 1,217 Dec-11-2020, 07:07 PM
Last Post: tester_V
  helping PyInstaller To Find files Harshil 0 613 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 1,571 Aug-25-2020, 01:52 AM
Last Post: tester_V
  Project files, Python files Heyjoe 4 1,024 Jun-27-2020, 07:30 PM
Last Post: Heyjoe
  python 3 find difference between 2 files pd007 2 896 May-22-2020, 01:16 AM
Last Post: Larz60+
  Find all “*.wav” files that created yesterday on linux host with python. pydeev 6 2,325 Jan-07-2020, 06:43 AM
Last Post: pydeev
  finding yesterday and tomorrrow without using date.time module apexman 10 2,721 Feb-25-2019, 05:33 AM
Last Post: samsonite
  python script cant find folder where files are kept Shameendra 2 1,421 Nov-23-2018, 04:46 PM
Last Post: Shameendra

Forum Jump:

User Panel Messages

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