Posts

Showing posts with the label write large file

Python code to generate random string

Today I wrote a small piece of Python code to generate random string. The string will have only the characters: 0-9, A-Z, a-z. word = '' for i in range(wordLen): word += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') Here is the complete source code that generates a large file that has a random word one per line. import random def get_random_word(wordLen): word = '' for i in range(wordLen): word += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') return word def main(): fout = open('word_list.txt', 'w') for i in range(100000000): wordLen = random.randint(5, 15) word = ran_word(wordLen) if i % 500000 == 0: fout.close() fout = open('word_list.txt', 'a') print i fout.write(word+"\n") fout.close() main()