Practice on file handing

Write a Python program to append text to a file and display the text

1 Like

file_name = “example.txt”

append = input("Enter text to append to the file: ")

with open(file_name, ‘w’) as file:
file.write(append)
print(f"Text appended to {file_name} successfully.")
with open (file_name,“r”) as txt:
print(txt.read())

user_Statement = input("Enter text to append: ")

with open(“my_file.txt”, “a+”) as file:
file.write(user_Statement + “\n”)

with open(“my_file.txt”, “r”) as file:
contents = file.read()
print(contents)