Python Forum
Filtering files, for current year files
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Filtering files, for current year files
#1
Greetings!
I want to filter files (copy those later) created at the beginning of 2021( somewhere in the beginning).
No errors produced but also not filtering Confused

import datetime
from pathlib import Path

today = datetime.datetime.today()

myRd = 'f:\\'
for sb1 in Path(myRd).iterdir() :
    if sb1.is_dir :
        for sb2 in Path(sb1).iterdir() :
            if sb2.is_file :
                print(f" Files ONLY --> {sb2}")
                fst = sb2.stat().st_mtime
                ft_time = datetime.datetime.fromtimestamp(fst) - today
                if ft_time.days < 220 :
                    print (f" 2021 file -->>{ft_time}")
Thank you.
Reply
#2
There's a good site with many examples for date comparisons here: https://pymotw.com/3/datetime/
Reply
#3
Have you tried printing out the ft_time.days for some of the files in your directory? I think you'll find that they're all negative and therefore all less than 220.
Reply
#4
I'm confused,
Why you sent me to the date/time manipulation website (great site by the way, I know about it)
I need a date-time comparison.

The code fails in the --> 'IF' block.
Also, I noted files are having negative days" -230" days, and so on...
                if ft_time < 213 :
                    print (f" 2021 file -->>{ft_time}")
Error:
if ft_time < 213 :
TypeError: '<' not supported between instances of 'datetime.timedelta' and 'int'


Thank you.
Reply
#5
I think that error message can’t be clearer. You just need to make values compareable (timedelta to timedelta or int to int)
tester_V likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#6
(Aug-03-2021, 03:46 AM)tester_V Wrote: Also, I noted files are having negative days" -230" days, and so on...

But you are not comparing them to -230, you're comparing them to a positive number (220) on line 14. All the negative numbers will be less than that, so you will not filter any files.

You're basically showing all files that have a modification date that is not past 220 days in the future.
tester_V likes this post
Reply
#7
You can shorten it, if you use glob with wildcards.
def walk_subdirectories(root, max_age_days):
    for path in Path(root).glob("*/*"):
        if path.is_file and path.exists():
            mtime = datetime.datetime.fromtimestamp(path.stat().st_mtime)
            mtime_delta = datetime.datetime.now() - mtime
            mtime_days = mtime_delta / datetime.timedelta(days=1)
            if mtime_days < max_age_days:
                print(f" Files ONLY --> {path}")
                print (f" 2021 file -->> {mtime_days:.0f}")
I swapped mtime and now, to get positive timedeltas.
Another trick is abs(datetime1 - datetime2) which will always return a positive value.

You should read this article about datetime and arithmetic: https://realpython.com/python-datetime/
The globbing is explained here: https://www.malikbrowne.com/blog/a-begin...b-patterns
And you can apply this on path objects. There is also a rglob method on path objects, which searches recursive and machtes the pattern. In your case, you just need the glob method.
The glob pattern "*/*" will find "root" / "some_directory" / "some_file".

This line calculates the total days, and it's a float.
mtime_days = mtime_delta / datetime.timedelta(days=1)
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#8
Hi,
I appreciate the examples with the "def" but I'm trying to avoid it for now. It makes the code complicated and hard to read/understand. I want to make it simple, even I will understand it Wink

I'm trying to compare a Timestamp of a file with the CutOff day and if Timestamp is "bigger number" than CutOff -print

But it doesn't print.
Or should I compare the "age of the file-number of days" instead of the timestamps?

import datetime
from pathlib import Path

today = datetime.datetime.today()
one_day = datetime.timedelta(days=1)
tom = today - one_day*213

print(f" Cut OFF Day -> {tom}")
days=1
myRd = 'D:\\'
for sb1 in Path(myRd).iterdir() :
    if sb1.is_dir :
        for sb2 in Path(sb1).iterdir() :
            if sb2.is_file :
                #print(f" Files ONLY --> {sb2}")
                fst = sb2.stat().st_mtime

                ft_time = datetime.datetime.fromtimestamp(fst) - today
                print(f" Age of the file -> {ft_time}")
                
                ft_time = datetime.datetime.fromtimestamp(fst)
                print(F" FIle created ->{ft_time}")
                if ft_time > tom:
                    print(f" 2021 files -> {ft_time}")
Thank you.
Reply
#9
to DeaD_EyE,
Thank you for the code! it is amazing, I do not know what to say....
And special thanks for the coaching and the links to the useful websites!
I'm sure there are a lot of folks here that really appreciate your help and your style of coding and coaching...
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Correct/proper way to create save files snakes 0 463 Mar-11-2025, 06:58 PM
Last Post: snakes
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 7 3,345 Mar-09-2025, 04:25 PM
Last Post: Pedroski55
  how to download large files faster? kucingkembar 3 817 Feb-20-2025, 06:57 PM
Last Post: snippsat
  Inserting Python Buttons into KV Files edand19941 3 503 Feb-19-2025, 07:44 PM
Last Post: buran
Question [SOLVED] Right way to open files with different encodings? Winfried 3 4,155 Jan-18-2025, 02:19 PM
Last Post: Winfried
  Applications config files / Best practices aecordoba 2 1,967 Oct-23-2024, 12:56 PM
Last Post: aecordoba
  Compare 2 files for duplicates and save the differences cubangt 2 955 Sep-12-2024, 03:55 PM
Last Post: cubangt
  Convert Xls files into Csv in on premises sharpoint Andrew_andy9642 3 1,021 Aug-30-2024, 06:41 PM
Last Post: deanhystad
  deleting files in program files directory RRADC 6 2,985 Aug-21-2024, 06:11 PM
Last Post: snippsat
  I'm trying to merge 2 .csv files with no joy! Sick_Stigma 3 941 Aug-03-2024, 03:20 PM
Last Post: mariadsouza362

Forum Jump:

User Panel Messages

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