Error writing data to file in python
2 answers
This problem can be completely eliminated by using the with statement :
with open("key.txt","w") as f:
s=str(a)
f.write(s)
The file will be automatically closed when the block is finished. By using with instruction , you don't need to worry about this type of errors creeping into your code.
+2
a source to share