miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

assign datetime string to a file name

Thu Dec 19, 2019 5:26 pm

Each time the camera takes a photo I'd like the saved file to have datetime.jpg as the file name.
I can arrange the elements of datetime into a string to print, but how can I use the variable to create a file with that unique name?
Thanks

User avatar
PeterO
Posts: 5623
Joined: Sun Jul 22, 2012 4:14 pm

Re: assign datetime string to a file name

Thu Dec 19, 2019 5:32 pm

Code: Select all

name = "19-12-19"

f = open(name,"w")
f.write("Some text\n")
f.close()  
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Thu Dec 19, 2019 6:18 pm

Many thanks, simplicity is best.
I got really confused with long winded answers in StackOverflow.

miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Thu Dec 19, 2019 6:45 pm

Ah, slight problem. how do I include the name in the file path.

f = open ('/media/pi/usb_stick/name',"w")

This just creates a file called "name".

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

Re: assign datetime string to a file name

Thu Dec 19, 2019 7:09 pm

Try this as an example

Code: Select all

import time

name = "/home/pi/" + time.strftime("%b-%d-%Y_%H:%M:%S") + ".txt"
print (name)
f = open(name,"w")
f.write("Some text\n")
f.close()  
produces a file in the directory /home/pi called " Dec-19-2019_19:06:08.txt " ( this the name updates on each run of the program producing a new file).
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Thu Dec 19, 2019 7:29 pm

Excellent.
Many thanks.

RDS
Posts: 739
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: assign datetime string to a file name

Thu Dec 19, 2019 7:37 pm

If you want to keep them in Chronological Order, use numbers for Year, Month, Day in that order, for example 20191219. Using letters will jumble your files up.

Paul Hutch
Posts: 471
Joined: Fri Aug 25, 2017 2:58 pm
Location: Blackstone River Valley, MA, USA
Contact: Website

Re: assign datetime string to a file name

Thu Dec 19, 2019 8:29 pm

Here's a format string variation for chronological sorting.

Code: Select all

"%Y-%m-%d_%H:%M:%S"



miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Thu Dec 19, 2019 9:27 pm

I’ve yet to sort the hardware, but the idea is to have this as a script that runs on boot, with the pi being switched on and off using the witty Pi controller. Takes one pic every 30 mins or so for a year.
To keep the files in order my intended file naming is, %m%h%D%M%y
Files will be saved to a memory stick, which is removed every month or so, during an off period, and files transferred to desktop machine.
That’s the plan so far.

gordon77
Posts: 4634
Joined: Sun Aug 05, 2012 3:12 pm

Re: assign datetime string to a file name

Thu Dec 19, 2019 9:36 pm

To keep in order l would suggest


%Y%m%d%H%M%S'

RDS
Posts: 739
Joined: Tue Oct 06, 2015 8:17 am
Location: Lancashire, UK

Re: assign datetime string to a file name

Thu Dec 19, 2019 9:51 pm

I think we have already tried that Gordon. He obviously wants to file in a random type of order! :D

miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Thu Dec 19, 2019 10:31 pm

The method of naming is unimportant because the files will be listed by date created.

User avatar
PeterO
Posts: 5623
Joined: Sun Jul 22, 2012 4:14 pm

Re: assign datetime string to a file name

Fri Dec 20, 2019 8:26 am

miketr3 wrote:
Thu Dec 19, 2019 10:31 pm
The method of naming is unimportant because the files will be listed by date created.
That's not really a very strong argument.... You are ignoring good advice and could making life more difficult for yourself than it has to be.
The evidence so far is that you are not an experienced programmer, and the people offering you advice can see avoidable problems ahead....

Is there a good reason for the format you have chosen ?
What processing of a directory full of the files will it make easier ?

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

miketr3
Posts: 44
Joined: Thu Jan 26, 2017 9:02 am

Re: assign datetime string to a file name

Fri Dec 20, 2019 8:49 am

I don't dispute the advice given, for which I am grateful, it makes sense.
Batch processing of the files will be based on date the file was created.
I'm only using the Pi to collect the files, processing will be done on a desktop machine.
In such a list of file names, because I read from left to right, to me it made sense to show the file name as minute/hour/day/month/year.
There may well be many instances where the naming format is critical, but I don't believe so in this case.
Screen Shot 2019-12-20 at 08.40.12.png
Screen Shot 2019-12-20 at 08.40.12.png (45.51 KiB) Viewed 334 times

User avatar
PeterO
Posts: 5623
Joined: Sun Jul 22, 2012 4:14 pm

Re: assign datetime string to a file name

Fri Dec 20, 2019 10:34 am

I would put this advice under the heading "Design for Debugging".

Some design alternatives may not be optimal but make debugging much easier. For example when I developed a simple remote procedure call (RPC) protocol I used text based messages on the network. This made debugging easier as a simple dump of network packets was all that was need to see what was going on.

This is why logging should always be in a text format so it can easily be viewed and searched !

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

Return to “Python”