Reputation: 670
Python beginner here: I wrote a script which creates a file every 3 minutes, I used strftime to set the name according to date/time it was created:
dt = datetime.now()
dtTemp = dt.strftime('%d-%b-%Y - %H-%M-%S')
filename = '/home/pi/baby_lapse/%s.jpg' % dtTemp
Here's an example of the output:
18-Jan-2019 - 23-21-03.jpg
The problem is that once I have more than one month of files, it creates a problem to sort the files by file name, which is important to me.
To resolve it, I thought to add some auto-number before the strftime string so it will produce an output such as:
000 - 18-Jan-2019 - 23-21-03.jpg
001 - 18-Jan-2019 - 23-24-03.jpg
002 - 18-Jan-2019 - 23-27-03.jpg
How can it be achieved?
Upvotes: 0
Views: 863
Reputation: 2210
I think the better idea would be to change the date formatting into numeric format such as %Y-%m-%d %H:%M:%S
. By doing so, it would be easy to sort file by name. For example,
/home/pi/baby_lapse/2019-01-26 20:51:42.jpg /home/pi/baby_lapse/2019-01-26 20:51:43.jpg
and so on ...
Just tried a sample script considering your case, I got a more readable result
Code
for _ in range(10):
dt = datetime.now()
dtTemp = dt.strftime('%Y-%m-%d %H:%M:%S')
filename = '/home/pi/baby_lapse/%s.jpg' % dtTemp
print(filename)
time.sleep(0.5)
Result
/home/pi/baby_lapse/2019-01-26 20:51:42.jpg
/home/pi/baby_lapse/2019-01-26 20:51:43.jpg
/home/pi/baby_lapse/2019-01-26 20:51:43.jpg
/home/pi/baby_lapse/2019-01-26 20:51:44.jpg
/home/pi/baby_lapse/2019-01-26 20:51:44.jpg
/home/pi/baby_lapse/2019-01-26 20:51:45.jpg
/home/pi/baby_lapse/2019-01-26 20:51:45.jpg
/home/pi/baby_lapse/2019-01-26 20:51:46.jpg
/home/pi/baby_lapse/2019-01-26 20:51:46.jpg
/home/pi/baby_lapse/2019-01-26 20:51:47.jpg
Upvotes: 0
Reputation: 670
I decided to follow @chepner suggestion and used 2019-01-18
as the date format.
After setting the date format for future records, I had to run a data fix and fix the naming of the existing records.
I ended up writing my own script that converts file names from this 18-Jan-2019 - 23-21-03.jpg
format to 2019-01-18 - 23-21-03.jpg
, I'm sharing it in case someone has a similar scenario:
import os
Months = {
"Jan": "01",
"Feb": "02",
"Mar": "03",
"Apr": "04",
"May": "05",
"Jun": "06",
"Jul": "07",
"Aug": "08",
"Sep": "09",
"Oct": "10",
"Nov": "11",
"Dec": "12"
}
for filename in os.listdir("."):
originalDateTime = filename.split(' ') #example: 18-Jan-2019 - 23-21-03.jpg
date = originalDateTime[0] #18-Jan-2019
datesplit = date.split('-') # '18', 'Jan', '2019'
dayOfMonth = datesplit[0] #18
month = datesplit[1] #Jan
year = datesplit[2] #2019
newFileName = year + '-' + Months.get(month, "none") + '-' + dayOfMonth + ' - ' + originalDateTime[2]
print newFileName # 2019-01-18 - 23-21-03
os.rename(filename, newFileName)
Upvotes: 0
Reputation: 2615
If you just prepend the milliseconds since epoch at the beginning, it will always sort by date. For ease of reading, you can leave the human-readable date string there.
To get milliseconds since epoch just use time.time() * 1000
.
Upvotes: 1