How to append text into a (file.txt) file in python
Here is my program code:
file = open("file.txt", "a")
aa = 10
bb = 1
cc = 12
xx = 20
try:
if xx > aa:
file.write("Yes xx is greater than aa")
file.close()
elif xx < aa:
file.write("No xx is not greater than aa")
file.close()
if aa > xx:
file.write("Yes aa is greater than xx")
file.close()
elif aa < xx:
file.write("No aa is not greater than xx")
file.close()
if cc > xx:
file.write("Yes cc is greater than xx")
file.close()
elif cc < xx:
file.write("No cc is not greater than xx")
file.close()
except Exception as e:
print(e)
and the output is:
My problem is why is only first if and elif condition work and write that string into text file and rest of condition run successfully but string not append in that file.
I would be very grateful if you could tell me how can I resolve this problem.