Professional Documents
Culture Documents
Day 7
Day 7
file our
operating system helps us
in create
write
w opens a file for writing creates the
file if it does not exist
In create creates a specified file error if
file already exists
In addition we can specify if the file should
be handled as binary or tent mode
t Text value Text mode
Default
b mode
Binary Binary eg images
a file object
we have few methods for the file object
read C method
Print f read C
we can only read once After reading we have
to close it
f open r path a
f write
f close C
f open r path w
4
f write
f close C
we need to close after writing
Create mode se
f close C
Print f read C
any issues
help of os module
import Os
we can use this to
Os remove file path
delete the file
the files
OS chair new site path changes the directory
to the new file path
OS rm dir file path s removes the folder
specified
This is ctrl delete we cannot restore it
Exception Handling
STEYN
E
Exceptions
4 Machines Interpreter
source code
Python Virtual
may
Python Engine
blocks
The fry block lets you test a block of code
for errors
not
happens
Syntax
try
try block
we
off
ex try
printer s n is not defined here
Name Error
try
cannot divided
to Number be
by zero
try
2 1,2 3,4
Syntax
except_Exception class
we print a message
we will have n no
of Exception classes
which will know what kind of error it is
any we want
message
ext except zero Division Errors
ext try
N a Sdf
Yo
N 20
Print hi
it like
ext try
N a Sdf
Yo
N 20
Print hi
were raised
Print hi
else
print no error
ex try
N a Sdf
Yo
N 20
Print hi
else
print no error
finally
print The try except is finished
not writable
try
f open demo file tut r
try
f write Lerum Ipsum
except
while
print something went wrong
writing to file
finally
f close c
except
went wrong when
print something
opening the file
ex i n hello