In my code I create a text file of random numbers. But I am wondering if it is possible to generate multiple files, each time with different ranges for the random numbers within a for loop.
I have an idea for how to implement this, but I'm not sure how to call the file I make each time testn, with n being the current value of n. So I would have files: test1, test2 and so on.
My implementation so far is:
numberOfFiles = 5 #set this to the number of files you want to make
for n in range(numberOfFiles):
newFile = open('testn.txt', 'w') #if possible I want to create a file each time and call it testn, where n is the current value of n.
x = []
for i in range(10): #number of lines in each file
x.append(random.randint(0,60 + (n * 10))
for val in range(len(x)):
newFile.write(str(x[val]) + "\n")
newFile.close()
with open(...) as newFile:newFile.close()and your files are automatically closed, even if there is an exception.