Carcigenicate

Record Time

Mar 29th, 2014
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. from time import time
  2.  
  3. save_path = '/storage/sdcard0/a.txt'
  4.  
  5. def get_time (seconds):
  6.     if seconds > 0:
  7.         if seconds <= 60:
  8.             return str (seconds) + " seconds"
  9.         elif seconds > 60 and seconds < 3600:
  10.             return str ((seconds) / 60) + " minutes"
  11.         elif seconds > 3600 and seconds < 86400:
  12.             return str ((seconds) / 3600) + " hours"
  13.         elif seconds > 86400:
  14.             return str ((seconds) / 86400) + " days"
  15.         else:
  16.             raise ValueError ("Bad Number")
  17.  
  18. lasttime = 0.0
  19.  
  20. with open (save_path, mode = 'a', encoding = 'utf-8') as checkfile:
  21.     #Create file if it doesnt already exist
  22.     checkfile.write ("")
  23.  
  24. with open (save_path, mode = 'r+', encoding = 'utf-8') as readfile:
  25.     #Read old time
  26.     temp = readfile.read ()
  27.     if temp:
  28.         lasttime = temp
  29.     else:
  30.         print ("Save file is empty")
  31.  
  32. lasttime = float (lasttime)
  33.  
  34. print ("Its been", get_time (time () - lasttime))
  35.  
  36. saveq = input ("Overwrite time? (y / n)\n")
  37. if saveq.lower () == 'y':
  38.     with open (save_path, mode = 'w+', encoding = 'utf-8') as writefile:
  39.         #Save new time if the user wants
  40.         writefile.write (str (time ()))
  41.         print ("Saved", end = ", ")
  42.  
  43. print ("Goodbye")
Advertisement
Add Comment
Please, Sign In to add comment