How to append date stamp to a log file created in Python

11072
1
02-09-2011 01:31 PM
MickClifford
New Contributor
Hi,

I have a python script which runs as a scheduled task each day, on a Windows 2008 server machine, which creates a log file.  However, I cannot get the log file to be created with a date appended to it.  Other CMD scripts use "update_logfile_%date:~10,4%%date:~7,2%%date:~4,2%.log" but this doesn't work.  I also tried "update_logfile_%Y-%m-%d.out", but that didn't work either.

Ideally, I would like something like this:
update_logfile_2011_02_10.out

Could anyone help please?

Many Thanks,

Mick
Tags (2)
0 Kudos
1 Reply
ChrisSnyder
Regular Contributor III
I do it like:

scriptName = sys.argv[0].split("\\")[-1][0:-3] #the script without the .py extension  
dateTimeStamp = time.strftime('%Y%m%d%H%M%S') #in the format YYYYMMDDHHMMSS
userName = string.lower(os.environ.get("USERNAME"))
logFile = rootDir + "\\" + scriptName + "_" + userName + "_" + dateTimeStamp + ".log"


So the log file is named something like: "super_pivot_v93_csny490_20110209134455.log"
0 Kudos