0% found this document useful (0 votes)
4 views4 pages

Week 6 File Handling and Exception Handling

The document provides an overview of file handling and exception handling in Python. It demonstrates various file operations such as reading, writing, appending, and handling exceptions like ZeroDivisionError and FileNotFoundError. Additionally, it includes user-defined exceptions and the use of try-except blocks to manage errors gracefully.

Uploaded by

blytonjohn45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Week 6 File Handling and Exception Handling

The document provides an overview of file handling and exception handling in Python. It demonstrates various file operations such as reading, writing, appending, and handling exceptions like ZeroDivisionError and FileNotFoundError. Additionally, it includes user-defined exceptions and the use of try-except blocks to manage errors gracefully.

Uploaded by

blytonjohn45
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

# File handling

#Opening the file in r mode- returns eroor if doesnt exist


#Syntax f = open(filename, mode)
f=open('python.txt','r')#print (f.read())

print (f.read(5))
for line in f:
print(line)

#creating a file
# write mode overwrites the exixting file if it exists or creates a new
one if it doesnt
f1=open('python.txt','w')
f1.write("This is the last chapter")
f1.write("We are learning file handling")
f1.close()

f2=open('python2.txt','w')
f2.write("This is the last chapter")
f2=open('python2.txt','r')
print(f2.read())

#append lines to a file


f3=open('python2.txt','a')
f3.write(" \n This is added to the file at end")
f3.close()

#open in read write mode and take file pointer back to start
file=open('test.txt','w+')
file.write("Hello world")
file.seek(0)
data=file.read()
print(data)
file.close()

#read write mode r+


file=open('test.txt','r+')
file.write("Hello world")
#file.seek(0)
data=file.read()
print(data)
file.close()

#a+ reading and writing


file=open('test.txt','a+')
file.write("Hello world!!!!!!!!!!")
#file.seek(0)
data=file.read()
print(data)
file.close()

#read ist line of file


file=open('test.txt','r')
#to print ist line
line=file.readline()
# to print all lines
for line in file:
print(line)

file=open('test.txt','r')
print(file.readlines())
file.close()

L = ["This is Delhi \n", "This is Paris \n", "This is London \n"]

# Creating a file
file1=open("myfile.txt", "w")
# Writing data to a file
file1.write("Hello \n")
file1.writelines(L)
file1.close()
f= open("myfile.txt", "r")
print(f.read())

Exception handling in python

#exceptions in Python
y=20
print(y)
i=2
if i==0:
raise Exception("i should not be zero")
x=y/i # ZeroDivisionError
print(x)
try:
x=10
y=0
x=x/y
print(x)
except ZeroDivisionError as zd_error:
print(zd_error)

# gives FileNotFoundError
#f=open("abc.txt",'r')
#f.read()

print("hello")
#print(y)) #syntax error
#Try : To keep code segment under check. kit executes till ist
exception is encountered
#except: segment to handle exeption after catching it
#else: run this when no exception exists
#finally: run this no matter what happens(excption or no exception)
# raise: to throw an exception if a condition occurs.
try:
f=open("abc.txt",'r')
f.read()
print("inside try")
except:
print("Couldnt open the file")
print("hiiiii")
try:
f=open("abcde.txt",'r')
f.read()
except FileNotFoundError as fnf_error:
print(fnf_error)

#try except else


try:
f=open("abcd.txt",'w+')
f.write("hello world")
#data=f.read()
except FileNotFoundError as fnf_error:
print(fnf_error)
else:
print("Printing file contents")
f=open("abcd.txt",'r')
data=f.read()
print(data)

finally:
print("End of program")
#user defined exception
class InvalidMarks(Exception):
pass
marks=10
try:
if marks<0 or marks>100:
raise InvalidMarks

except InvalidMarks:
print("Enter marks between 0 and 100")
else:
print("Marks=",marks)

#other way
marks1=2000
if marks1<0 or marks1>200:
raise Exception("Invalid marks!. Enter marks between 1 to 200")
print(marks1)

#learning Exception in python


def fun(a,b):
try:
c=(a+b)/(a-b)
except ZeroDivisionError as zero_div:
print(zero_div)
else:
print(c)
finally:
print("i will always execute")

fun(2,3)
fun(3,3)
fun(2,4)

You might also like