Class 12 Computer Science
File Handling in Python
Summary
- A file is a named location on a secondary storage media where data are permanently stored for later access.
- A text file contains only textual information consisting of alphabets, numbers and other extensions like .txt, .py, .c, .csv, .html, etc. Each byte of a text file represents a character.
- Each line of a text file is stored as a sequence of ASCII equivalent of the characters and is terminated by a special character, called the End of Line (EOL).
- Binary file consists of data stored as a stream of bytes.
- open() method is used to open a file in Python and it returns a file object called file handle. The file handle is used to transfer data to and from the file by calling the functions defined in the Python’s io module.
- close() method is used to close the file. While closing a file, the system frees up all the resources like processor and memory allocated to it.
- write() method takes a string as an argument and writes it to the text file.
- writelines() method is used to write multiple strings to a file. We need to pass an iterable object like lists, tuple etc. containing strings to writelines() method.
- read([n]) method is used to read a specified number of bytes (n) of data from a data file.
- readline([n]) method reads one complete line from a file where lines are ending with a newline (\n). It can also be used to read a specified number (n) of bytes of data from a file but maximum up to the newline character (\n).
- readlines() method reads all the lines and returns the lines along with newline character, as a list of strings.
- tell() method returns an integer that specifies the current position of the file object. The position so specified is the byte position from the beginning of the file till the current position of the file object.
- seek()method is used to position the file object at a particular position in a file.
- Pickling is the process by which a Python object is converted to a byte stream.
- dump() method is used to write the objects in a binary file.
- load() method is used to read data from a binary file.
Class 12 Computer Science
File Handling in Python
NCERT Exercise with Solution
1. Differentiate between:
a) text file and binary file
b) readline() and readlines()
c) write() and writelines()
Answer: (a) Text File and Binary File:
Text File
A text file contains only textual information consisting of alphabets, numbers and other extensions like .txt, .py, .c, .csv, .html, etc. Each byte of a text file represents a character.
Each line of a text file is stored as a sequence of ASCII equivalent of the characters and is terminated by a special character, called the End of Line (EOL).
Binary File:
Binary file consists of data stored as a stream of bytes i.e. as it is stored in computer memory (binary language). No translation occurs in binary file. In binary file no end of line translation. Faster than text file.
Answer: (b) readline() and readlines():
readline([n]) method reads one complete line from a file where lines are ending with a newline (\n). It can also be used to read a specified number (n) of bytes of data from a file but maximum up to the newline character (\n).
readlines() method reads all the lines and returns the lines along with newline character, as a list of strings.
Answer: c) write() and writelines()
write() method takes a string as an argument and writes it to the text file.
writelines() method is used to write multiple strings to a file. We need to pass an iterable object like lists, tuple etc. containing strings to writelines() method.
2. Write the use and syntax for the following methods:
a) open()
b) read()
c) seek()
d) dump()
Answer: (a) open()
To open a file in Python, we use the open() function. The syntax of open() is as follows:
file_object= open(file_name, access_mode)
This function returns a file object called file handle which is stored in the variable file_object.
Example: myObject=open(“myfile.txt”, “a+”)
Answer: (b) read()
read( )method is used to read a specified number of bytes of data from a data file.
The syntax of read() method is:
file_object.read(n)
The usage of read() methodT
>>> myobject=open(“myfile.txt”,’r’)
>>> myobject.read(10)
‘Hello ever’
>>> myobject.close()
Answer: (c) seek()
seek( ) method is used to position the file object at a particular position in a file.
The syntax of seek() is:
file_object.seek(offset [, reference_point])
In the above syntax, offset is the number of bytes by which the file object is to be moved.
reference_point indicates the starting position of the file object. That is, with reference to which position, the offset has to be counted. It can have any of the following values:
0 – beginning of the file
1 – current position of the file
2 – end of file
By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file.
Answer: (d) dump()
dump() method is used to convert (pickling) Python objects for writing data in a binary file. The file in which data are to be dumped, needs to be opened in binary write mode (wb).
Syntax of dump() is as follows:
dump(data_object, file_object)
where data_object is the object that has to be dumped to the file with the file handle named file_object.
3. Write the file mode that will be used for opening the following files. Also, write the Python statements to open the following files:
a) a text file “example.txt” in both read and write mode
Answer: “r+, “
file_handle = open(“example.txt”, “r+”)
b) a binary file “bfile.dat” in write mode
Answer: “wb”
file_handle = open(“bfile.dat”, “wb”)
c) a text file “try.txt” in append and read mode
Answer: “a+”
file_handle = open(“try.txt”, “a+”)
d) a binary file “btry.dat” in read only mode.
Answer: “rb”
file_handle = open(“btry.dat”, “rb”)
4 .Why is it advised to close a file after we are done with the read and write operations? What will happen if we do not close it? Will some error message be flashed?
Answer: Python file is advised to close, due to following reasons –
(a) Closing files, is a good programing practice.
(b) Sometimes, the last date is not written in file, it is held in memory. So when we give the close() command, it will write all data in the file. It saves from damage of file.
Generally in Python files gets closed automatically, If you not closed the file.
No, Python will not flashed any error message , if you does not close the file.