UNIT 4 Python
UNIT 4 Python
• The file handling plays an important role when the data needs to
be stored permanently into the file.
• The types of activities that you can perform on the opened file
are controlled by Access Modes.
Ankita Sharma CSE unit 4 2
Types of Files in Python In Python, there are two types of files. They are:
● Text files
● Binary files
Text files store the data in the form of characters. For example, if we store
the employer name "Ganesh", it will be stored as 6 characters and the
employee salary 8900.75 is stored as 7 characters. Normally, text files are
used to store characters or strings. (EG;.txt,.csv,.pdf)
Binary files store entire data in the form of bytes, i.e. a group of 8 bits
each. For example a character is stored as a byte and an integer is stored
in the form of 8 bytes (on a 64 bit machine). When the data is retrieved
from the binary file, the programmer can retrieve the data as bytes.
Binary files can be used to store images, audio and video
In Python, you use the open() function with one of the following options –
"x" or "w" – to create a new file:
f.close()
Ankita Sharma CSE unit 4 7
File Access Modes
This function inserts the string into the text file on a single line.
f.write(str) In this way, write() can be used to store a character or a
group of characters (string) into a file represented by the file object ‘f’.
f= open(r'File_Path', 'r‘)
Once opened, we can read all the text or content of the file using the
read() method. You can also use the readline() to read file line by line or
the readlines() to read all lines.
For example, content = f.read()
• We can read the first few set of lines from a file by using the
readline() method. Run a loop fo n times using for loop and range()
function, and use the readline() method in the loop’s body.
• line = fp.readline()This reads the next line from the file and updates the
variable line.The loop will continue processing lines until there are no
more lines to read
Ankita Sharma CSE unit 4 29
Reading First and the Last line using readline() :
We can get the first line by just calling the readline() method as
this method starts reading from the beginning always and we can
use the for loop to get the last line.
This method will return the entire file contents. The reading of the
contents will start from the beginning of the file till it reaches the EOF
(End of File).
This method will internally call the readline() method and store the
contents in a list. The output of this method is a list.
By using the read() function we can read the content of the file.
What if we want to read the file from a specific position or find out
from where the reading begins? Python’s seek() and tell() functions
come in handy here.
Ankita Sharma CSE unit 4 34
• A file handle or pointer denotes the position from which the file
contents will be read or written. File handle is also called as file pointer
or cursor.
• For example, when you open a file in write mode, the file pointer is
placed at the 0th position, i.e., at the start of the file. However, it
changes (increments) its position as you started writing content into it.
• Or, when you read a file line by line, the file pointer moves one line at
a time.
• How many points the pointer will move is computed from adding
offset to a reference point; the reference point is given by the
whence argument
The default value for the whence is the beginning of the file, which
is 0
Note:
Open file in binary mode. For reading use the rb, for writing use the
wb, and for both reading and writing use rb+.
Convert byte to string if you are reading a text file.
• For example, move to the 10th character from the end of the file. This
can be done by setting the offset corresponding to the end of the file.
f = open(“myfile1.txt", "r")
# Second parameter is by default 0
# sets Reference point to twentieth
# index position from the beginning
f.seek(20)
# prints current position
print(f.tell())
print(f.readline())
f.close() Ankita Sharma CSE unit 4 50
Example 2: Seek() function with negative offset only works when
file is opened in binary mode. Let’s suppose the binary file contains
the following text.
Solution :
• There are no arguments for this method. The return value is the
integer representing the file handle position.