find 'yesterdays 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: find 'yesterdays files' (/thread-33995.html) |
find 'yesterdays files' - tester_V - Jun-16-2021 Greetings! I guess the question is not new, but I cannot find a clan small snippet. I found one here, on this forum - it has a function and 25 lines of code, it is not small or clean. Anyway, need to scan a directory and find (and copy) files created yesterday. I got this so far: import os import time import datetime today = datetime.date.today() td = today.ctime() print(f" Today --> {td}") dirtoscan = "c:\\0001\\" for fl in os.listdir(dirtoscan) : f_path = os.path.join(dirtoscan,fl) modified = os.path.getmtime(f_path) dateModif = time.ctime(modified) print(f"File Modif --> {dateModif}")I could split 'td' get a Second Element of spit td = today.ctime()And Split 'datemodif' to get Second Element of split dateModif = time.ctime(modified)and compare them, but it looks awful, ugly kind of code... Thank you. RE: find 'yesterdays files' - Gribouillis - Jun-16-2021 You could use pathlib and datetime modules >>> from pathlib import Path >>> import datetime as dt >>> >>> yesterday = dt.datetime.now().date() - dt.timedelta(days=1) >>> f = Path('foo.png') >>> if yesterday == dt.datetime.fromtimestamp(f.stat().st_mtime).date(): ... print('f was last modified yesterday') ... >>>Note that st_mtime gives modification time on all platforms, and st_ctime gives creation time in Windows but not necessarily in Linux. RE: find 'yesterdays files' - Yoriz - Jun-16-2021 With the following code, i made a created_yesterday_stat_matcher function, you could make different functions to match different stats.import datetime from pathlib import Path def created_yesterday_stat_matcher(path_stat): ctime = datetime.date.fromtimestamp(path_stat.st_ctime) return ctime == datetime.date.today() - datetime.timedelta(days=1) def directory_stat_matcher_files(directory, stat_matcher): for path in Path(directory).iterdir(): if stat_matcher(path.stat()): yield path directory = 'your directory' for path in directory_stat_matcher_files(directory, created_yesterday_stat_matcher): print(path) RE: find 'yesterdays files' - tester_V - Jun-17-2021 Well, I tried Gribouillis snipped and it works. I have a question. I have already a variable 'path' I tried to use it f=pathbut the code fails. I'm wondering what does it mean? f=Path from pathlib import Path import datetime as dt yesterday = dt.datetime.now().date() - dt.timedelta(days=1) dirtoscan = "c:\\0001\\" for fl in os.listdir(dirtoscan) : path = os.path.join(dirtoscan,fl) #f = path f = Path(path) if yesterday == dt.datetime.fromtimestamp(f.stat().st_mtime).date(): print(f"File was last modified yesterday - {f}")I did not try another snippet, it looks complicated, I do not understand it. Thank you! RE: find 'yesterdays files' - Gribouillis - Jun-17-2021 You could simplify this in for f in Path(dirtoscan).iterdir(): ...but you should perhaps consider the case where dirtoscan contains items that are not ordinary files, such as directories... Also programming is not only copying and pasting working snippets. Try to understand what's going on. RE: find 'yesterdays files' - tester_V - Jun-17-2021 I just posted a simple version of the script I'm working on. I do a test if an item in the 'listdir'is is a directory or a file... One more question if I may, the capital "Path", what is it? Path(dirtoscan)Thank you both of you for the snippets! RE: find 'yesterdays files' - Yoriz - Jun-17-2021 It is the Path class imported from the pahtlib from pathlib import Path https://docs.python.org/3/library/pathlib.html Wrote:pathlib — Object-oriented filesystem paths RE: find 'yesterdays files' - snippsat - Jun-17-2021 (Jun-17-2021, 05:24 PM)tester_V Wrote: One more question if I may, the capital "Path", what is it?Also look at. Python 3's pathlib Module: Taming the File System Quote:Since Python 3.4, pathlib has been available in the standard library. RE: find 'yesterdays files' - tester_V - Jun-18-2021 I did not know that... Thank you! You are Da Man! |