Advertisement
Python253

load_dictionary

Jun 1st, 2024
699
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.72 KB | None | 0 0
  1. """
  2. Helper file for use with 'Impractical_Python_Projects/Chapter_3'
  3.  
  4. Load a text file as a list.
  5.  
  6. Arguments:
  7. -text file name
  8.  
  9. Exceptions:
  10. -IOError if filename not found.
  11.  
  12. Returns:
  13. -A list of all words in text file in lower case.
  14.  
  15. Requires-import sys
  16.  
  17. """
  18. import sys
  19.  
  20. def load(file):
  21.     """Open a text file & turn contents into a list of lowercase strings."""
  22.     try:
  23.         with open(file) as in_file:
  24.             loaded_txt = in_file.read().strip().split('\n')
  25.             loaded_txt = [x.lower() for x in loaded_txt]
  26.             return loaded_txt
  27.     except IOError as e:
  28.         print("{}\nError opening {}. Terminating program.".format(e, file),
  29.               file=sys.stderr)
  30.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement