I'm working on a joke that involves permutations, to create a list of words, in several separate files. Problems start appearing when words with more than 4 letters are required, because the corresponding file becomes too heavy. What I wanted was that every time a given file arrived at 499 999 (for example) it opened another file with the same name but with an extension. For example 4-character words " wl4_1.txt
, wl4_2.txt
..." where " wl4_2.txt
" is the continuation of " wl4_1.txt
", and the first line of this (" wl4_2.txt
") would be the line of 500 000 of " wl4_1.txt
", that is, we would close " wl4_1.txt
" and we would continue our list in " wl4_2.txt
".
This code below works beautifully, I just wanted to add this functionality that I explained. In this example are 3 letter words and is already heavy (830 584 lines):
import itertools
import string
def main():
alphabet = string.letters + string.digits + string.punctuation
alphaLen = len(alphabet)
print alphabet
for i in range(3):
NumToPerm = i+1 #remove 0 from the permutations function
fileTest = open("word_lists/wl" +str(NumToPerm)+ ".txt", "w")
perm(fileTest, alphabet, NumToPerm)
def perm(fileTest, alphabet, NumToPerm):
for p in itertools.product(alphabet, repeat=NumToPerm):
word = str(p)
for char in word:
if char in " (),'":
word = word.replace(char,'')
fileTest.write(word+ '\n')
fileTest.close()
main()