I hope, after last week's blog post, you've had a good weekend renaming all of the files on your (or a friend's) computer. Feels good, doesn't it? Seeing them all listed in chronological order like that. Almost as good as a Sunday morning trip to the tip.
But where do you go from here? Prepare to have your mind blown.
What if - what if - there was a way of naming your files like this automagically? What if - imagine, just for a moment - you could reap the benefits of time-ordered filenames without all the brain-strain of checking what the date and time is yourself? Well, if you're making your files in Python, there is. Try the following script:
We've used the Python
time library to get the current time, create a string in the ISO 8601 extended format, and write it to a file with a filename that conforms to our cunning chronological criteria. Try copying the above into a script (something like
my_time.py) and run it with:
$ python my_time.py
Run it multiple times, and when you list the directory contents you'll get:
$ ls 2016-02-06-125526Z_my-file.txt 2016-02-07-162304Z_my-file.txt 2016-02-08-150944Z_my-file.txt
Admitedly, for files that only contain a sentence telling you what the time was when they were created, this isn't going to change the world. But when you've got a data or log file for which the time is important, it might save you a bit of time later.