Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.18 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. #s3my0n
  4.  
  5. def print_files(files_number=2):
  6.     files = []
  7.  
  8.     for i in xrange(files_number):
  9.         try:
  10.             a = raw_input('\nEnter file %d: '%(i+1))
  11.             fopen = open(a, 'r')
  12.         except IOError:
  13.             print '\nCould not open file %s'%(a)
  14.             exit(1)
  15.         except KeyboardInterrupt:
  16.             print '\nAborted: exiting'
  17.             exit(1)
  18.         else:
  19.             files.append(fopen)
  20.  
  21.     for i in files:
  22.         print '\n%s\n%s%s' % ('-'*50, i.read(), '-'*50)
  23.  
  24. def ask_for_files_number():
  25.     while True:
  26.         try:
  27.             number = int(raw_input("\nPlease enter the number of files to read: "))
  28.         except ValueError:
  29.             print '\nNeed a valid number'
  30.             continue
  31.         except KeyboardInterrupt:
  32.             print '\nAborted'
  33.             exit(1)
  34.         else:
  35.             return number
  36.  
  37. if __name__ == '__main__':
  38.    
  39.     print_files() # initial print
  40.    
  41.     while True:
  42.         try:
  43.             choice = raw_input("\nWould you like to read more files? Enter 1 for yes or 2 for no: ").lower()
  44.         except KeyboardInterrupt:
  45.             print '\nAborted'
  46.             exit(1)
  47.         else:
  48.             if choice in ('1', 'yes'):
  49.                 n = ask_for_files_number()
  50.                 print_files(n)
  51.             elif choice in ('2', 'no'):
  52.                 print 'Good bye'
  53.                 exit(0)
  54.             else:
  55.                 print 'Please enter a valid choice (1/yes, 2/no)'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement