ProjectTitan313

File I/O Test

Jun 23rd, 2013
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.55 KB | None | 0 0
  1. """
  2. Testing ground for file input and output.
  3. Learning the fun way of course.
  4.  
  5. == Changelog ==
  6.  
  7. 6/18/13: Started. Timestamp() function created.
  8. 6/23/2013: After obnoxious amounts of fail, Timestamp() update works.
  9.  
  10. """
  11.  
  12. # Modules imported. datetime for Timestamp(), re for regex functions.
  13.  
  14. from datetime import datetime
  15. import re
  16.  
  17. # Global variables defined.
  18.  
  19. Now = datetime.now() # really ugly format by default, so I prettied it in Timestamp().
  20. CurrentYear = Now.year
  21. CurrentMonth = Now.month
  22. CurrentDay = Now.day
  23. CurrentHour = Now.hour
  24. CurrentMinute = Now.minute
  25. CurrentSecond = Now.second
  26.  
  27. # Timestamp function formats and prints current date and time.
  28. # Adds a new line, then the stamp, instead of smashing them together now.
  29.  
  30. def Timestamp():
  31.     Date = (str(CurrentMonth) + '/' + str(CurrentDay) +  '/' + str(CurrentYear)) # month/day/year
  32.     Time = (str(CurrentHour) + ':' + str(CurrentMinute) + ':' + str(CurrentSecond)) # hour:minute:seconds
  33.  
  34.     return "Last modified: " + str(Date) + " at " + str(Time)
  35.  
  36. # Opens the test file to be used; creates it if it doesn't exist in proper directery.
  37. # 'a' is 'append'; adds data from file.write() to the end of file without overwriting.
  38. # 'w' will overwrite the entire freaking file. 'r' is read only, 'r+' is read+write.
  39.  
  40. Test = open('test.txt', 'r+')
  41.  
  42. # Checks if test.txt already contains a time stamp. If not, it is appended to the file.
  43.  
  44. if re.search('Last modified: [\d]+/[\d]+/[\d]+ at [\d]+:[\d]+:[\d]+', Test.read()) == None:
  45.     Test = open('test.txt', 'a')
  46.     Test.write('\n' + str(Timestamp()))
  47.     print("Timestamp added")
  48.  
  49. Test = open('test.txt', 'r+')
  50.  
  51. # Holy shit the timestamp updates :O
  52.  
  53. OriginalFile = Test.read() # Original file before any edits.
  54. # Timestamp() format, to recognize and replace. [\d]+ means "At least one integer here"
  55. TestCompile = re.compile('Last modified: [\d]+/[\d]+/[\d]+ at [\d]+:[\d]+:[\d]+')
  56. # Original file + new Timestamp() edit.
  57. NewFile = re.sub(TestCompile, str(Timestamp()), OriginalFile)
  58. # Clears the entire file, because this appends a copy of entire file even though its not set to append.
  59. Test.write('')
  60. Test.close()
  61. Test = open('test.txt', 'r+')
  62. # Replace entire original file with the new, updated file.
  63. Test.write(NewFile)
  64.  
  65.  
  66. """
  67. ALWAYS make sure file is closed when finished.
  68. Python will write any and all file.write() data to a buffer, then the file,
  69. and only save the changes when file is closed. The code below closes then
  70. double checks closed status.
  71. """
  72.  
  73. Test.close()
  74.  
  75. if Test.closed == False:
  76.     Test.close()
Advertisement
Add Comment
Please, Sign In to add comment