fork download
  1. import os
  2. from time import strftime
  3.  
  4. # Function calls are expensive, let's just remember this format string
  5. time_format = '%A, %B %d, %Y [%H:%M:%S]'
  6.  
  7. def addLine(text):
  8. # This general function will handle all our file writing
  9. with open(filepath, 'w+') as f:
  10. f.write(text +
  11. '\nThis line was added by %s on %s\n' %
  12. (name, strftime(time_format)) # We want the current time
  13.  
  14.  
  15. # Prompt for name
  16. name = raw_input('What is your first name?\n> ')
  17. filepath = name + '.txt'
  18.  
  19. # If the don't say "yes" they said "no"
  20. if not os.path.isfile(filepath) or raw_input('File already exists, overwrite? (Y/N)')[0].upper() == 'Y':
  21. # First just add the user's name
  22. addLine(name + ' was here')
  23.  
  24. # Now we loop to add lines until asked to stop
  25. while True:
  26. # Output current file
  27. print 'The file now reads:'
  28. with open(filepath) as f:
  29. # We can just loop through the file rather than using file.readlines
  30. for line in f:
  31. print '\t' + line.rstrip()
  32.  
  33. if raw_input('\nWould you like to add more lines? (Y/N)')[0].upper() != 'Y':
  34. break # End of looping
  35. # Prompt for and add new text
  36. addLine(raw_input('What would you like to add?\n'))
  37.  
  38. else:
  39. print 'File not created.'
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty