Advertisement
aolivens

Python Word List Generator

Sep 10th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. #=====================================#
  2. #==============MODULES================#
  3. #=====================================#
  4.  
  5. import itertools
  6.  
  7. #=====================================#
  8. #==============VARIABLES==============#
  9. #=====================================#
  10.  
  11. alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()_+-={}|[]\:';,./<>? "
  12.  
  13. #=====================================#
  14. #==============FUNCTIONS==============#
  15. #=====================================#
  16.  
  17. #defines algorithm to create words list
  18. def wordlist(alpha):
  19.     for n in itertools.count(1):
  20.         for word in itertools.product(alpha, repeat = n):
  21.             yield ''.join(word)
  22.  
  23. #creates output file and writes list to file
  24. def buffer_function(filename, alpha, max_words):
  25.     file = open(filename, "w")
  26.    
  27.     buffer = ""
  28.     buffer = list(itertools.islice(wordlist(alpha), max_words))
  29.     for word in buffer:
  30.         file.write("%s\n" % word)
  31.     print "Word List '%s' Created" % filename
  32.    
  33.     file.close()
  34.     buffer = ""
  35.    
  36. #=====================================#
  37. #=============MAIN PROGRAM============#
  38. #=====================================#
  39.  
  40. def main():
  41.     print "This program generates a word list using a pre-defined alphabet and writes the output to a file."
  42.     print
  43.     print
  44.    
  45.     max_words = input("Maximum number of words in list: ")
  46.     filename = raw_input("Enter a filename to save the word list as: ") + ".txt"
  47.    
  48.     buffer_function(filename, alpha, max_words)
  49.    
  50. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement