Skip to content

Instantly share code, notes, and snippets.

@optroodt
Created November 15, 2019 10:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save optroodt/7afef122f276f38553b9e1e56812072e to your computer and use it in GitHub Desktop.
Save optroodt/7afef122f276f38553b9e1e56812072e to your computer and use it in GitHub Desktop.
Generate 1000 text files with random words
import pathlib
import random
dictionary = pathlib.Path("/usr/share/dict/web2") # It's there on MacOS
words = set()
with dictionary.open("r") as f:
for l in f:
words.add(l.strip())
subfolder = pathlib.Path("./files")
if not subfolder.is_dir():
subfolder.mkdir(exist_ok=True)
for i in range(0, 1000):
txt_file = pathlib.Path("files/file_{:04d}.txt".format(i))
size = 0
with txt_file.open("w") as f:
while size < 1_000_000:
words_sample = random.sample(words, 1000)
for word in words_sample:
word_plus_space = word + " "
f.write(word_plus_space)
size += len(word_plus_space)
if size > 1_000_000:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment