How to name my file based on a randomly generated number?

I want to name my file as the student number comes up.

Here is the code:

import random
SNumber=random.randint(199999,999999)
print(SNumber)
writefile=open("studentNumberhere.txt","a")
writefile.write(SNumber)
writefile.close()

If the code runs and it generates the number: 123456

The filename would be 123456.txt

And if I generate another number, there will be another file appearing on my folder.


import random
SNumber=random.randint(199999,999999)
print(SNumber)
writefile = open(str(SNumber)+'.txt','a')
writefile.write(str(SNumber))
writefile.close()