Python - how to convert ctime to '%m/%d/%Y %H:%M:%S'
Question: Is there any direct way to convert ctime value to '%m/%d/%Y %H:%M:%S' format?

For example, convert "Wed Nov 6 15:43:54 2013" to "11/06/2013 15:43:54"

I tried the following but does not give me the format I want, which is
"11/06/2013 15:43:54":

    
    
    >>> t = time.ctime()
    >>> f = datetime.datetime.strptime(t, '%a %b %d %H:%M:%S %Y')
    >>> print f
    2013-11-06 15:43:54
    

But if I pass it directly to time.strftime, it'll want 9-item sequence:

    
    
    >>> n = time.strftime(t, '%D %H:%M:%S')
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: argument must be 9-item sequence, not str
    


Answer: In your example, you could just use `datetime.now`:

    
    
    from datetime import datetime
    
    d = datetime.now()
    d.strftime('%m/%d/%Y %H:%M:%S')
    Out[7]: '11/06/2013 18:59:38'
    

But. If you're taking in a `ctime` style string from somewhere else, parse it
with `datetime.strptime` then format it in the way you want using datetime's
`strftime` (not `time`'s).

    
    
    from datetime import datetime
    import time
    d = datetime.strptime(time.ctime(),"%a %b %d %H:%M:%S %Y")
    
    d.strftime('%m/%d/%Y %H:%M:%S')
    Out[9]: '11/06/2013 19:01:11'
    

