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
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 943 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  File loop curiously skipping files - FIXED mbk34 10 685 Feb-10-2024, 07:08 AM
Last Post: buran
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Class test : good way to split methods into several files paul18fr 4 403 Jan-30-2024, 11:46 AM
Last Post: Pedroski55
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 398 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  Upload Files to Azure Storage Container phillyfa 6 581 Dec-22-2023, 06:11 AM
Last Post: Pedroski55
  merge all xlsb files into csv mg24 0 303 Nov-13-2023, 08:25 AM
Last Post: mg24
  Newbie question about switching between files - Python/Pycharm Busby222 3 543 Oct-15-2023, 03:16 PM
Last Post: deanhystad
  Move Files based on partial Match mohamedsalih12 2 745 Sep-20-2023, 07:38 PM
Last Post: snippsat
  open python files in other drive akbarza 1 632 Aug-24-2023, 01:23 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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