简体   繁体   中英

Python Generate a big text file

Using Python 3.8, I am trying to generate a big text file filled by random alphabets and or letters. Here is the code that does not do anything, and I am not too sure why.

def generate_big_random_letters(filename,size):
    
    import random
    import string

    chars = ''.join([random.choice(string.letters) for i in range(size)]) #1


    with open(filename, 'w') as f:
        f.write(chars)
    pass

if __name__ == '__main__':
    generate_big_random_letters("temp_big_letters.txt",1024*1024)

Use string.ascii_letters and string.digits :

import random
import string

def generate_big_random_letters(filename, size):
    chars = ''.join([random.choice(string.ascii_letters + string.digits) for i in range(size)])  # 1

    with open(filename, 'w') as f:
        f.write(chars)

if __name__ == '__main__':
    generate_big_random_letters("temp_big_letters.txt", 1024 * 1024)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM