Created
November 15, 2019 10:44
-
-
Save optroodt/7afef122f276f38553b9e1e56812072e to your computer and use it in GitHub Desktop.
Generate 1000 text files with random words
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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