Skip to main content

/docs/images/banner.jpg

Python

File Operations

Write a program that demonstrates file operations in Python.

import os

def append_to_file(filename, content):
with open(filename, "a") as file:
file.write(content + "\n")

def delete_file(filename):
os.remove(filename)
print('File deleted successfully!')

filename = "test.txt"

append_to_file(filename, "This is some appended text.")

delete_file(filename)
note

To compile and run the program, you can use the following commands:

python3 foo.py
note

You require a file named test.txt in the same directory as the program to run the program.