Filtering files, for current year 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: Filtering files, for current year files (/thread-34469.html) |
Filtering files, for current year files - tester_V - Aug-03-2021 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 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. RE: Filtering files, for current year files - Larz60+ - Aug-03-2021 There's a good site with many examples for date comparisons here: https://pymotw.com/3/datetime/ RE: Filtering files, for current year files - bowlofred - Aug-03-2021 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.
RE: Filtering files, for current year files - tester_V - Aug-03-2021 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. RE: Filtering files, for current year files - perfringo - Aug-03-2021 I think that error message can’t be clearer. You just need to make values compareable (timedelta to timedelta or int to int) RE: Filtering files, for current year files - bowlofred - Aug-03-2021 (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. RE: Filtering files, for current year files - DeaD_EyE - Aug-03-2021 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-beginners-guide-glob-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) RE: Filtering files, for current year files - tester_V - Aug-03-2021 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 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. RE: Filtering files, for current year files - tester_V - Aug-07-2021 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... |