Skip to content

Instantly share code, notes, and snippets.

@Darkflib
Created December 3, 2024 18:05
Show Gist options
  • Save Darkflib/5824920031e8544066796cb449dad401 to your computer and use it in GitHub Desktop.
Save Darkflib/5824920031e8544066796cb449dad401 to your computer and use it in GitHub Desktop.
import random
import os
def load_words():
# Get a random word from the system dictionary.
# Check if the file exists and is readable.
if not os.access('/usr/share/dict/words', os.R_OK):
return []
# Check if the file is not empty.
if os.stat('/usr/share/dict/words').st_size == 0:
return []
else:
print('Found words file of size ' + str(os.stat('/usr/share/dict/words').st_size) + ' bytes.')
with open('/usr/share/dict/words') as f:
words = f.read().splitlines()
print('Loaded ' + str(len(words)) + ' words.')
return words
words_list = load_words()
def get_random_word():
if not words_list:
return None
return random.choice(words_list)
print(get_random_word() + ' ' + get_random_word() + ' ' + get_random_word())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment