Hard Fought Knowledge


Parenting

Inspirational Quotes

My Quotes


Enemies and allies change. Do you job, do it professionally and never hate.



Apache

Linux

Eclipse

Python : How to rename a file if it exists:

This will rename file by inserting an integer time stamp between the extension and the base name. It does consider that a file may have more than one period in the file name and moves right to left for an extension

Import os

Import time

 

ts = int(time.time())

filename = "/home/user/desktop/file.db"

 

if os.path.isfile(filename): os.rename(filename,str(os.path.dirname(filename)) + '/' + os.path.basename(filename).rsplit('.',1)[0] + "_" + str(ts) + '.' + str(os.path.basename(filename).split('.')[-1:][0]))

 

 




© Colin Bitterfield 2019