1

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?

Legomert99
  • 21
  • 3

5 Answers5

4

The error is, that you don't close the file after writing into it. So there is nothing written. One solution is to use context manager (with keyword) that automatically closes the file:

import random
import datetime

with open("test.txt", "w") as w:
    for n in range(0, 2):
        w.write(str(random.randint(0, 10000)))
        w.write(",")
    w.write(str(random.randint(0, 10000)))

with open("test.txt", "r") as f:
    myArray = f.read().split(",")

for i in range(0, len(myArray)):
    myArray[i] = int(myArray[i])

print(myArray)

Prints:

[3998, 6615, 7496]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
2

You might have a better time using a machine-readable format such as JSON:

import random
import json

numbers = [random.randint(0, 10000) for x in range(3)]

with open("test.txt", "w") as f:
    json.dump(numbers, f)

# ...

with open("test.txt", "w") as f:
    numbers = json.load(f)

print(numbers)
AKX
  • 152,115
  • 15
  • 115
  • 172
1

Make sure to close the file after writing and before reading. Else you will be reading an empty file. Best way to do this is to use file as a context manager.

with open("test.txt", "w") as w:
    for n in range(0, 2):
        w.write(str(random.randint(0,10000)))
        w.write(",")
    w.write(str(random.randint(0,10000)))

with open("test.txt", "r") as f:
    myArray = list(map(int, f.read().split(',')))

print(myArray)
flakes
  • 21,558
  • 8
  • 41
  • 88
0

You need to flush what you wrote to the file. When the file is closed it is flushed automatically. Using with could avoid calling close manually:

with open("test.txt", "w") as w:
    for n in range(0,2):
        w.write(str(random.randint(0,10000)))
        w.write(",")
    w.write(str(random.randint(0,10000)))

reference: How often does python flush to a file?

Z Li
  • 4,133
  • 1
  • 4
  • 19
0

You can ry changing this part:

f = open("test.txt", "r")
myArray = f.read().split(',')
for i in range (0,len(myArray)):
    myArray[i] = int(myArray[i])

to:

f = open("test.txt", "r")
myArray = f.read().split(',')
myArray = list(map(int, filter(None, myArray)))

Using the built-in filter() method can filter out empty strings, and the built-in map() method maps every element in the give iterator with the given function.

Red
  • 26,798
  • 7
  • 36
  • 58