Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
find 'yesterdays files'
#1
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.
Reply
#2
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.
tester_V likes this post
Reply
#3
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)
tester_V likes this post
Reply
#4
Well, I tried Gribouillis snipped and it works.
I have a question.
I have already a variable 'path'
I tried to use it
f=path
but 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!
Reply
#5
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.
tester_V likes this post
Reply
#6
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!
Reply
#7
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
This module offers classes representing filesystem paths with semantics appropriate for different operating systems. Path classes are divided between pure paths, which provide purely computational operations without I/O, and concrete paths, which inherit from pure paths but also provide I/O operations.
Reply
#8
(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.
With pathlib, file paths can be represented by proper Path objects instead of plain strings as before.
These objects make code dealing with file paths:
  • Easier to read, especially because / is used to join paths together
  • More powerful, with most necessary methods and properties available directly on the object
  • More consistent across operating systems, as peculiarities of the different systems are hidden by the Path object
In this tutorial, you have seen how to create Path objects, read and write files,
manipulate paths and the underlying file system, as well as some examples of how to iterate over many file paths.
tester_V likes this post
Reply
#9
I did not know that... Confused
Thank you!
You are Da Man! Wink
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find and replace in files with regex and Python Melcu54 0 378 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 927 Dec-11-2020, 07:07 PM
Last Post: tester_V
  helping PyInstaller To Find files Harshil 0 522 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,272 Aug-25-2020, 01:52 AM
Last Post: tester_V
  python 3 find difference between 2 files pd007 2 746 May-22-2020, 01:16 AM
Last Post: Larz60+
  Find all “*.wav” files that created yesterday on linux host with python. pydeev 6 1,925 Jan-07-2020, 06:43 AM
Last Post: pydeev
  python script cant find folder where files are kept Shameendra 2 1,278 Nov-23-2018, 04:46 PM
Last Post: Shameendra
  find hidden python files bobsmith76 4 1,770 Oct-23-2018, 10:57 PM
Last Post: wavic
  Where can I find a little project example using main.py and other files sylas 7 2,866 Aug-31-2017, 02:59 PM
Last Post: metulburr
  How to make a script to find a certain word in text files in a whole directory ? RandoomDude 2 4,285 Apr-27-2017, 10:27 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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