joeboy69
Posts: 7
Joined: Fri May 17, 2019 6:58 pm

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

Thu Aug 15, 2019 2:06 pm

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.

User avatar
topguy
Posts: 5626
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

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

Thu Aug 15, 2019 2:16 pm

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

joeboy69
Posts: 7
Joined: Fri May 17, 2019 6:58 pm

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

Thu Aug 15, 2019 3:00 pm

@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.

scotty101
Posts: 3649
Joined: Fri Jun 08, 2012 6:03 pm

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

Thu Aug 15, 2019 3:09 pm

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.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

pcmanbob
Posts: 6450
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

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

Thu Aug 15, 2019 4:39 pm

Also answered in the duplicate thread https://www.raspberrypi.org/forums/view ... 1&t=248906
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

joeboy69
Posts: 7
Joined: Fri May 17, 2019 6:58 pm

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

Fri Aug 16, 2019 7:07 am

@scotty101 thank you so much Brother, its working fine now. Appreciate your kind help ;)

Return to “Python”