![]() |
Have to use Python 2.x to find "yesterday' files - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Have to use Python 2.x to find "yesterday' files (/thread-34942.html) |
Have to use Python 2.x to find "yesterday' files - tester_V - Sep-18-2021 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! RE: Have to use Python 2.x to find "yesterday' files - bowlofred - Sep-18-2021 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: ... RE: Have to use Python 2.x to find "yesterday' files - DeaD_EyE - Sep-18-2021 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 listdirThen 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.
RE: Have to use Python 2.x to find "yesterday' files - tester_V - Sep-18-2021 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! RE: Have to use Python 2.x to find "yesterday' files - tester_V - Sep-18-2021 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! RE: Have to use Python 2.x to find "yesterday' files - bowlofred - Sep-18-2021 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. RE: Have to use Python 2.x to find "yesterday' files - tester_V - Sep-19-2021 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! |