I'm trying to generate a file containing a short list of random numbers. Every time I run this code I get a file where the list is truncated, usually in the middle of a number. I'm using PyCharm.

```
import random

def generate_test(l, r):
    lst = []
    for x in range(1,l):
        lst = lst + [random.randrange(0, r)]
    return lst
test = generate_test(100, 10**12)
test_file = open("test.txt", "wb")
test_file = open("test.txt", "r+")
test_file.write(str(test))

```