python - How get a datetime string to suffix my filenames? -


I need a function to create a datafile name with a suffix, should be the current date and time.

I want to date, February 18, 2014 15:02:

  data_201402181502.txt  

But that's what I get Is: Data_2014218152.txt

My code ...

  import time prefix_file = 'data' tempus = time.localtime (time .time ()) suffix = Str (tempus.tm_year) + str (tempus.tm_mon) + str (tempus.tm_mday) + str (tempus.tm_hour) + str (tempus.tm_min) name_file = prefix_file + '_' + suffix + '.txt'  

Leading padding, which you can use for this Zero examples are on month:

  import from time strftime name_file = "{0 } {Code}   If you start an integer again for the string using, then it will not have leading zeros:  str (2) == '2' . You can specify it using the  str.format  syntax:  "{0: 02d}". Format (2) == '02' . 


Comments