1. File open in write mode ("w")
file = open("example.txt", "w") # "w" = write mode
file.write("Hello AIkiPadhai!\n")
file.close()
2. File open in append mode ("a")
file = open("example.txt", "a") # "a" = append mode
file.write("This is new line.\n")
file.close()
3. Writing multiple lines
lines = ["Line 1\n", "Line 2\n", "Line 3\n"]
with open("example.txt", "w") as file:
file.writelines(lines)
4. Using with statement (recommended)
with open("example.txt", "a") as file:
file.write("Appended using with statement.\n")
Next Step
Ab hum seekhenge List Methods, jisme Python ke useful list functions ko use karenge.