Page 1 of 1

Grab the last file (recently) created file (image) to be send to Telegram

Posted: Thu Aug 15, 2019 2:06 pm
by joeboy69
Hi good day everyone, can someone help me with the python coding as per the situation stated below? Please give me some solutions so that I can sort it out. Thanks in advance :)
Situation: Pi cam will capture images with ( date & exact time) and store it in a folder. From that folder, I need to retrieve/grab the last captured image to be sent to my Telegram.

Re: Grab the last file (recently) created file (image) to be send to Telegram

Posted: Thu Aug 15, 2019 2:16 pm
by topguy
Is the files (images) named in some way so that its easy to find the latest one ? Or is it just one file rewritten over and over again ?

keywords for googling together with "python"..
- "inotify" ( for monitoring a file or folder for changes )
- "fstat" ( for checking creation/modification date/time on files )

And the Telegram API:
https://github.com/python-telegram-bot/ ... to-the-API

Re: Grab the last file (recently) created file (image) to be send to Telegram

Posted: Thu Aug 15, 2019 3:00 pm
by joeboy69
@topguy files (images) are named (generated automatically) in some way (data= time.strftime("%d_%b_%Y_%H_%M_%s")) so that its easy to find the latest one.

Re: Grab the last file (recently) created file (image) to be send to Telegram

Posted: Thu Aug 15, 2019 3:09 pm
by scotty101
You can find the most recently created file (rather than using name of the file) using the following command.

Code: Select all

import glob
import os
newest = max(glob.iglob('*.jpg'), key=os.path.getctime)
print(newest)
glob.glob('*.jpg') will find all files with a jpg extension in the current folder
max( will return the path of the file modified most recently
key=os.path.getctime tells the max function to get the "ctime" for each file and to find the largest time value.

Re: Grab the last file (recently) created file (image) to be send to Telegram

Posted: Thu Aug 15, 2019 4:39 pm
by pcmanbob
Also answered in the duplicate thread https://www.raspberrypi.org/forums/view ... 1&t=248906

Re: Grab the last file (recently) created file (image) to be send to Telegram

Posted: Fri Aug 16, 2019 7:07 am
by joeboy69
@scotty101 thank you so much Brother, its working fine now. Appreciate your kind help ;)