fileN = open("test1.txt", 'w')
print('255 255 255', file = fileN)
#fileN.close()
The file "test1.txt" would be empty without the final closing statement.
def foo():
fileN = open("test2.txt", 'w')
print('255 255 255', file = fileN)
#fileN.close()
foo()
However, if I wrote the code in a function and then call it, the file would be written successfully even without the closing the statement.
So I have 2 questions: Does a file have to be closed to be written? Will python close the file automatically when the function is over?