There has to be a better way to split this string!
rosuav at gmail.com
Tue Feb 9 20:34:54 EST 2016
On Wed, Feb 10, 2016 at 12:26 PM, Anthony Papillion
<anthony at cajuntechie.org> wrote:
> I am using datetime.now() to create a unique version of a filename.
First off, be aware that this won't make a unique file name. But if
you're okay with that (deal with collisions somehow), then sure.
> When the final file is named, it will look something like:
> Notice I'm replacing all of the "."'s, " "'s, and ":"'s returned by
> datetime.now() with "-"'s. I'm doing that using the following code but
> it's freaking ugly and I KNOW there is a better way to do it. I just
> can't seem to think of it right now. Can anyone help? What is the
> "right", or at least, less ugly, way to do this task?
Instead of using str(), use strftime():
>>> now = datetime.datetime.now()
You could instead use some other format string if you like. Here's your options:
More information about the Python-list