I'm writing a Python code to write random files to a file and read the same numbers from the file and put them to an int list
import random
import datetime
w = open("test.txt", "w")
for n in range(0,2):
w.write(str(random.randint(0,10000)))
w.write(",")
w.write(str(random.randint(0,10000)))
f = open("test.txt", "r")
myArray = f.read().split(',')
for i in range (0,len(myArray)):
myArray[i] = int(myArray[i])
print(myArray)
But when I run the code, I get error saying
Traceback (most recent call last):
File "main.py", line 13, in <module>
myArray[i] = int(myArray[i])
ValueError: invalid literal for int() with base 10: ''
How can I solve this? Is there any other way to write random numbers to a file and then put them to a list?