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?
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)