You open a file, and then you close it. You don't write anything in it though, because the function write() fails should anything but a variable of type str() be passed to it! Here is an alternative to write() with fewer restrictions:
with open('file_name.txt', 'w') as file: print(variable_you_want_written, file=file, end='\n')
print() function has fewer restrictions and is a better option here as it automatically formats the inputs it receives as a str(), regardless of the initial type(), and excluding functions, instances and generator objects, which would be displayed as, for instance, something like this: <function <lambda> at 0x1089b70d0>.
Additionally, be aware that the visual format to which they are converted (and subsequently displayed or written in a file) may not necessarily be to your liking, so I suggest you do experiment with the results and see what works best in a particular situation.
Click here for Python 3 docs on print().
msgto the file, wheremsgis equal tooperazionesincemsg = operazione.