Skip to main content

/docs/images/banner.png

Python

File handling in Python

WAP toto show the file handling in python

try:
f = open("file.txt", "r")
# there should be a file named file.txt in the same directory as this file
print(f.read())
except FileNotFoundError:
print("File not found")
else:
print("File read successfully")
finally:
print("End of program")

# Output:
# Hello, World!
# File read successfully
# End of program