Python Unit 4
Python Unit 4
A file operation can be done in the following order. To write some text to a file, we need to open the file
using the open method with one of the following access
o Open a file modes.
o Read or write - Performing operation
w: It will overwrite the file if any file exists. The file
o Close the file pointer is at the beginning of the file.
Creating files a: It will append the existing file. The file pointer is at
filehandle = open(‘kids’ , ‘w’) instructs the the end of the file. It creates a new file if no file exists.
operating system to create a file with name kids & return
its filehandle for the file. The argument w indicates that Example
file is opened for writing. fileptr = open("file2.txt", "w")
Syntax: fileptr.write(Python has an easy syntax and user
fileobject = open(<file-name>, <access friendly interaction)
mode>, <buffering>) fileptr.close()
Opening a file Close Files
Python provides an open() function that accepts two Once all the operations are done on the file, we
arguments, file name and access mode in which the must close it through our Python script using
file is accessed. The function returns a file object the close() method. Any unwritten information gets
which can be used to perform various operations like destroyed once the close() method is called on a file
reading, writing, etc. object.
Example
fileptr = open("file.txt","r") Syntax
if fileptr: fileobject.close()
print("file is opened successfully")
File Access Modes creates a new file if no file exists with the
same name.
Access Description
mode
ab It opens the file in the append mode in
binary format. The pointer exists at the
r It opens the file to read-only mode. The
end of the previously written file. It
file pointer exists at the beginning. The
creates a new file in binary format if no
file is by default open in this mode if no
file exists with the same name.
access mode is passed.
a+ It opens a file to append and read both.
rb It opens the file to read-only in binary
The file pointer remains at the end of
format. The file pointer exists at the
the file if a file exists. It creates a new file
beginning of the file.
if no file exists with the same name.
r+ It opens the file to read and write both.
ab+ It opens a file to append and read both
The file pointer exists at the beginning
in binary format. The file pointer remains
of the file.
at the end of the file.
rb+ It opens the file to read and write both
in binary format. The file pointer exists
at the beginning of the file. Common functions for accessing files are
w It opens the file to write only. It open(fn, 'w') fn is a string representing a file name.
overwrites the file if previously exists or Creates a file for writing and returns a file handle.
creates a new one if no file exists with open(fn, 'r') fn is a string representing a file name.
the same name. The file pointer exists at Opens an existing file for reading and returns a file
the beginning of the file.
handle.
wb It opens the file to write only in binary open(fn, 'a') fn is a string representing a file name.
format. It overwrites the file if it exists Opens an existing file for appending and returns a
previously or creates a new one if no file file handle.
exists. The file pointer exists at the fh.read() returns a string containing the contents of
beginning of the file. the file associated with file handle fh.
fh.readline() returns the next line in the file
w+ It opens the file to write and read both.
It is different from r+ in the sense that it associated with the file handle fh.
overwrites the previous file if one exists fh.readlines() returns a list each element of which is
whereas r+ doesn't overwrite the one line of the file associated with the file handle
previously written file. It creates a new fh.
file if no file exists. The file pointer exists fh.write(s) write the string s to the end of the file
at the beginning of the file. associated with the file handle fh.
fh.writeLines(S) S is a sequence of strings. Writes
wb+ It opens the file to write and read both
each element of S to the file associated with the file
in binary format. The file pointer exists
at the beginning of the file. handle fh.
fh.close() closes the file associated with the file
a It opens the file in the append mode. handle fh
The file pointer exists at the end of the
previously written file if exists any. It
OUTPUT:
file1.txt
2. OS module in python
The OS module in python provides special
functions and methods for operating system
functionality like filename extraction.
We can use the os.path.basename function to
get the filename from the path given by the user.
we can use the os.path.splitext function to get
the filename without the extension.
Example:
import os
file_path = "C:/Users/User1/Documents/file1.txt"
full_name = os.path.basename(file_path)
file_name = os.path.splitext(full_name)
print(full_name)
print(file_name[0])
Output:
file1.txt
file1
def Add(self,T):
R=TwoNum()
R.__x=self.__x+T.__x
R.__y=self.__y+T.__y
return R
obj1 = TwoNum()
obj2 = TwoNum()
Example: Example:
class Parent1:
class Calculation1:
def func1(self):
print ("function is defined inside the parent class.") def Summation(self,a,b):
return a+b;
class Child1(Parent1):
def func2(self): class Calculation2:
print ("function is defined inside the child class.") def Multiplication(self,a,b):
# Driver's code return a*b;
object = Child1()
object.func1() class Derived(Calculation1,Calculation2):
object.func2() def Divide(self,a,b):
return a/b;
Output: d = Derived()
Output:
Multiple Inheritance If a class is able to be created
30
from multiple base classes, this kind of Inheritance is 200
known as multiple Inheritance. When there is multiple 0.5
Inheritance, each of the attributes that are present in the
classes of the base has been passed on to the class that is Multilevel and Multipath Inheritance
derived from it. Multi-level inheritance is archived when a derived
class inherits another derived class. There is no limit
on the number of levels up to which, the multi-level
inheritance is archived in python.
Syntax
class Base1:
<class-suite>
class Base2:
<class-suite> Syntax
. class class1:
. <class-suite>
. class class2(class1):
class BaseN: <class suite>
<class-suite> class class3(class2):
class Derived(Base1, Base2, ...... BaseN): <class suite>
<class-suite> .
.
Example 1:
class example:
def __init__(self, X):
self.X = X
Output: