Iv - File Handling
Iv - File Handling
Iv - File Handling
FILE HANDLING
ITE103 – PROGRAMMING 2
What is file handling in Python?
◦Python File Handling is the process of creating,
reading, writing, updating, and deleting files in a
computer's file system.
◦File handling in Python is used to store data
permanently in a file or to read data from an existing
file.
C R U D
CREATE READ UPDATE DELETE
MISSION
Animated by the Gospel and guided by the teachings of the Church, it helps to uplift the
quality of life and to effect social transformation through:
1.Quality, Catholic, Paulinian formation, academic excellence, research, and community
services;
2.Optimum access to Paulinian education and service in an atmosphere of
compassionate caring; and,
3.Responsive and innovative management processes.
Opening a text file
file = open("iamspup.txt", "r")
content = file.read()
print(content)
file.close()
Let’s do it together!
import os
if os.path.exists(file_path):
os.remove(file_path)
print(f"{file_path} has been deleted.")
else:
print(f"{file_path} does not exist.")
Try this:
◦Create a Python program that prints all .txt files
in a directory, prompts the user to select a file
for deletion, and then deletes the selected file:
import os