with open("output.txt", "a") as file:
file.write("This is an additional line.\n")
print("Text appended successfully!")
#source code --> clcoding.com
Code Explanation:
Open the File in Append Mode ("a")
The open("output.txt", "a") statement opens the file in append mode.
If the file does not exist, Python creates it automatically.
If the file already exists, new content is added at the end without deleting existing data.
Append a New Line to the File
file.write("This is an additional line.\n") adds a new line at the end of the file.
The "\n" ensures the text appears on a new line instead of continuing on the same line.
Automatic File Closure
Using with open(...) ensures the file closes automatically after writing.
Print Success Message
print("Text appended successfully!") confirms that the operation was successful.
0 Comments:
Post a Comment