Python Forum
Need to identify only files created today.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to identify only files created today.
#1
Hi,
I need to copy a file if it was accessed/created today.
I could make a batch file for that but I want to use Python.
It is probably a combination of some kind of File Stat and
time.now()
.
not sure how to do that.
Thank you.
Reply
#2
Here's one way to go about it:

import os
from datetime import datetime

todays_date = datetime.now ()
today = todays_date.date ()
path = '/path/to/files/'

for filename in os.listdir (path) :
	file_time = os.path.getmtime (path + filename)
	file_date = datetime.fromtimestamp (file_time)
	file_day = file_date.date ()
	if file_day == today :
		print (f'File to copy is {path + filename}.')
tester_V likes this post
Reply
#3
A Generator-Example:

import datetime
from pathlib import Path


def find_files_today(path):
    today = datetime.date.today()
    for file in Path().rglob("*"):
        try:
            is_file = file.is_file() # <- possible PermissionError
        except PermissionError:
            continue
        if not is_file:
            continue
        mdate = datetime.date.fromtimestamp(file.stat().st_mtime)
        if is_file and today == mdate:
            yield file
        
tester_V likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
That's amazing! Dance
Thank you both!
There are at least two different ways to get today's file stat.
Love it!

from datetime import datetime
And :
import datetime
By the way, it seems both of these come from the same module:
import datetime
Could you elaborate, please?
I'm sure there are a lot of people who would live to understand it.
Thank you again!
Reply
#5
datetime is both the module name and the class name. If you import the module with:

import datetime
then you would have to use the class like this:

file_date = datetime.datetime.fromtimestamp (file_time)
if you import like this:
from datetime import datetime
Then you don't need to use datetime.datetime but only the class name, like this:

file_date = datetime.fromtimestamp (file_time)
tester_V likes this post
Reply
#6
Outstanding!
Thank you again for the code and the coaching!
I really appreciate it!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  identify not white pixels in bmp flash77 17 2,461 Nov-10-2023, 09:21 PM
Last Post: flash77
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 548 Oct-03-2023, 11:15 AM
Last Post: snippsat
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,017 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,860 Dec-06-2020, 04:14 PM
Last Post: Divya577
  Make list of dates between today back to n days Mekala 3 2,390 Oct-03-2020, 01:01 PM
Last Post: ibreeden
  Store Previous date to calculate delta from today Captain_Wolf 7 3,313 May-08-2020, 06:08 PM
Last Post: SheeppOSU
  Need to identify sheet color in excel workbook chewy1418 2 2,524 Feb-14-2020, 03:26 PM
Last Post: chewy1418
  Find all “*.wav” files that created yesterday on linux host with python. pydeev 6 4,784 Jan-07-2020, 06:43 AM
Last Post: pydeev
  Need help to identify Mersenne Primes, I do need a search pattern. Pleiades 0 1,924 Dec-03-2019, 11:05 PM
Last Post: Pleiades
  Syntax Error : I can't identify what's wrong! caarsonr 11 6,283 Jun-10-2019, 11:18 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

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