Class 12 FileHandling
Class 12 FileHandling
Differentiate between:
a) text file and binary file
Answer :-
a = A text file stores data as ASCII/UNICODE characters whereas a binary file stores
data in binary format (as it is stored in memory). Internal conversion is required in text
file and, hence, it is slower but binary file does not need any translation and so is faster.
b = The readline() function reads from a file in read mode and returns the next line in
the file or a blank string if there are no more lines. (The returned data is of string type.)
The readlines() function also reads from a file in read mode and returns a list of all lines
in the file. (The returned data is of list type.)
c = Write() function is used to write only one line while writelines is used to write more
than one line which is stored in form of list.
Answer :-
a = open( ) is use for excess file in read, write and append mode . Syntax :-
<variable> = open( <file name> , <mode of open of file >)
or
with open ( <file name> , <mode of open of file > ) as <variable> :
b = read() function is use for reading the data of files . Syntax :-
Answer :-
Closing the file releases the resources. On a large system running many process you
can use up all of the resources to keep track of open files. This may prevent any process
from opening another file. When the process ends or crashes the OS is supposed to
clean up for you.
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 special
symbols. Such files are stored with 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).
• 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.
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.
Answer :-
f = open("hello.txt","a")
f.write("Welcome my class")
f.write("It is a fun place")
f.write("You will learn and play")
f.close()
a) P = open("practice.txt","r")
P.read(10)
x = P.read()
Answer :-
In part "a" file P will read 10 characters but will not print anythings while in part "b" it will
read whole character present in practice file and store in variale "x".