fob=open('data1.txt','a')
fob.write("\nthis is 2nd line of text added")
You can open data1.txt and see the content inside. fob=open('data1.txt','a')
fob.write("\nthis is 2nd line of text added")
## Writting at the end of file is over , now read
fob=open('data1.txt','r')
print(fob.read())
fob.close()
Output is here
this is first line of text
this is 2nd line of text added
Use try-except to safely handle potential file errors:
try:
fob = open('data1.txt', 'a')
fob.write("\nNew line added.")
except IOError:
print("Error opening file.")
finally:
fob.close()
Write multiple lines of text in append mode:
with open('data1.txt', 'a') as fob:
fob.write("\nLine 1\nLine 2")
Appending logs for each program run:
with open('log.txt', 'a') as log_file:
log_file.write("Program executed successfully.\n")
1,Ravi,20,30,40,120
2,Raju,30,40,50,130
3,Alex,40,50,60,140
4,Ronn,50,60,70,150