Python: How to time script from beginning to end?
Question: I've created a script which processes files and returns the size of each file
in bytes. The time it takes is dependent on how large the directory is that
you scan.

At the top of my script, I placed in:

    
    
    now = "\n", strftime("%Y-%m-%d %H:%M:%S", gmtime())
    print "Started processing data at:",now
    

and at the bottom:

    
    
    finish = strftime("%Y-%m-%d %H:%M:%S", gmtime())
    print "Finished processing data at: ", strftime("%Y-%m-%d %H:%M:%S", gmtime())
    difference = now.strftime("%H:%M:%S", gmtime()) - finish.strftime("%H:%M:%S", gmtime())
    

I didn't think it was going to work, but I hope you understand my logic. I
want to subtract the `now` hours, minutes & seconds from the `finish` hours,
minutes & seconds.

What is the correct way of doing this?

Many thanks.


Answer: Try datetime module.

    
    
    from datetime import datetime
    start = datetime.now()
    

Later

    
    
    difference = datetime.now() - start
    

