DAY 9
Exceptional handling
If we write a code
a=int(input("Enter a
number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
b=int(input("Enter 2nd number"))
c=a/b
print(c)
run the function CTL+shift+fn+F10
as long as a and b values are non zero, it will deliver a
value
Enter a number4
Enter 2nd number2
2.0
However if the value
changes to zero then we get the output
Enter a number4
Enter 2nd number0
Traceback (most recent
call last):
File
"C:/Users/AbhishekSingh/PycharmProjects/First Project/venv/exc2.py",
line 3, in <module>
c=a/b
ZeroDivisionError:
division by zero
To handle this
exception we use Try and except commands
try:
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
print("Hello")
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
print("Hello")
run the function CTL+shift+fn+F10
Enter a number4
Enter 2nd number0
Value can't be zero
Or
Enter a numberasasasa
Enter only integer value
Hello
We use finally command
to make sure no what what exception occurs, final command gets executed
try:
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
finally:
print("Hello")
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
finally:
print("Hello")
Enter a number4
Enter 2nd number0
Value can't be zero
Hello
You cant remember all
the exception errors. So to take care of that we use standalone exception
try:
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
except:
print("Enter valid number")
finally:
print("Hello")
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
except:
print("Enter valid number")
finally:
print("Hello")
run the function CTL+shift+fn+F10
Enter a number6
Enter 2nd number0
Value can't be zero
Hello
You can use else command
also if no exception occurs in your program
try:
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
except:
print("Enter valid number")
else:
print("No Exception")
finally:
print("Hello")
a=int(input("Enter a number"))
b=int(input("Enter 2nd number"))
c=a/b
print(c)
except ZeroDivisionError:
print("Value can't be zero")
except ValueError:
print("Enter only integer value")
except:
print("Enter valid number")
else:
print("No Exception")
finally:
print("Hello")
run the function CTL+shift+fn+F10
Enter a number4
Enter 2nd number2
2.0
No Exception
Hello
FILE HANDLING
f=open("abc.txt","w")
the in the above line, open is used to open a file
if the file exists, if not then it will create a new file. It will write over
existing file if existing file exists
“w” is the command for writing, “r” is for reading
and “a” for appending
f=open("abc.txt","w")
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
run the function CTL+shift+fn+F10
abc.txt gets created with contents
This is first file program
I'm enjoying
This is python
I'm enjoying
This is python
f=open("abc.txt","r")
s=f.read()
print(s)
f.close()
s=f.read()
print(s)
f.close()
run the function CTL+shift+fn+F10
This is first file program
I'm enjoying
This is python
f=open("abc.txt","r")
s=f.readlines()
print(s)
f.close()
s=f.readlines()
print(s)
f.close()
run the function CTL+shift+fn+F10
['This is first file program\n', "I'm enjoying\n", 'This is
python']
f=open("abc.txt","r")
s=f.readline()
print(s)
f.close()
s=f.readline()
print(s)
f.close()
run the function CTL+shift+fn+F10
This is first file program
f=open("abc.txt","a")
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
run the function CTL+shift+fn+F10
This is first file program
I'm enjoying
This is pythonThis is first file program
I'm enjoying
This is python
I'm enjoying
This is pythonThis is first file program
I'm enjoying
This is python
To have read and write in same command
=open("abc.txt","w")
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
f=open("abc.txt","r")
s=f.read()
print(s)
f.close()
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.close()
f=open("abc.txt","r")
s=f.read()
print(s)
f.close()
run the function CTL+shift+fn+F10
This is first file program
I'm enjoying
This is python
Or use
W+
f=open("abc.txt","w+")
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.seek(0)
s=f.read()
print(s)
f.close()
f.write("This is first file program\n")
f.write("I'm enjoying\n")
f.write("This is python")
f.seek(0)
s=f.read()
print(s)
f.close()
run the function CTL+shift+fn+F10
This is first file program
I'm enjoying
This is python
In the above example we are using f.seek(0). It will
take back to zero index, if you change zero to 1 or 2 then it will print from 1
or 2 index
To read file from your desktop
path="C:\\Users\\AbhishekSingh\\Desktop\\f1.txt"
f=open(path,"r")
s=f.read()
print(s)
f.close()
f=open(path,"r")
s=f.read()
print(s)
f.close()
run the function CTL+shift+fn+F10
this is the new shit
If you look at the path
path="C:\\Users\\AbhishekSingh\\Desktop\\f1.txt"
normally path uses
single \ and not double\\ in python \
menas going to another line so we would need to make single \ to double \\ in
the path of the file
Renaming a file
import os
os.rename("abc.txt","cde.txt")
os.rename("abc.txt","cde.txt")
os is operating system
import os
src="C:\\Users\\AbhishekSingh\\Desktop\\f1.txt"
dst="C:\\Users\\AbhishekSingh\\Desktop\\f2.txt"
os.rename(src,dst)
src="C:\\Users\\AbhishekSingh\\Desktop\\f1.txt"
dst="C:\\Users\\AbhishekSingh\\Desktop\\f2.txt"
os.rename(src,dst)
import os
os.remove("C:\\Users\\AbhishekSingh\\Desktop\\f2.txt")
os.remove("C:\\Users\\AbhishekSingh\\Desktop\\f2.txt")
Learning Python- Day 10
No comments:
Post a Comment