Page 1 of 1

assign datetime string to a file name

Posted: Thu Dec 19, 2019 5:26 pm
by miketr3
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

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 5:32 pm
by PeterO

Code: Select all

name = "19-12-19"

f = open(name,"w")
f.write("Some text\n")
f.close()  
PeterO

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 6:18 pm
by miketr3
Many thanks, simplicity is best.
I got really confused with long winded answers in StackOverflow.

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 6:45 pm
by miketr3
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".

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 7:09 pm
by pcmanbob
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).

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 7:29 pm
by miketr3
Excellent.
Many thanks.

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 7:37 pm
by RDS
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.

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 8:29 pm
by Paul Hutch
Here's a format string variation for chronological sorting.

Code: Select all

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

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 8:36 pm
by gordon77

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 8:36 pm
by gordon77

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 9:27 pm
by miketr3
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.

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 9:36 pm
by gordon77
To keep in order l would suggest


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

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 9:51 pm
by RDS
I think we have already tried that Gordon. He obviously wants to file in a random type of order! :D

Re: assign datetime string to a file name

Posted: Thu Dec 19, 2019 10:31 pm
by miketr3
The method of naming is unimportant because the files will be listed by date created.

Re: assign datetime string to a file name

Posted: Fri Dec 20, 2019 8:26 am
by PeterO
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

Re: assign datetime string to a file name

Posted: Fri Dec 20, 2019 8:49 am
by miketr3
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 328 times

Re: assign datetime string to a file name

Posted: Fri Dec 20, 2019 10:34 am
by PeterO
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