DELHI PUBLIC SCHOOL, DURGAPUR
CLASS- XII
ASSIGNMENT- 4 (2025-26)
SUBJECT- COMPUTER
TOPIC- File Handling
Introduction to Data Files
A file (i.e., data file) is a named place on the disk drive which contains stream of bytes/sequence of
related data pertaining to a specific application.
In Python files, structure of data are not stored. Type of Files
1. Text File:- A text file (.txt) in Python contains sequence of lines where each line is a sequence of
charecters (ASCII or UNICODE), on permanent storage media. Each line is terminated by a special
character called End Of Line (EOL) or Line Delimiter. Text files are stored in human readable form
and they can also be created using any text editor.
2. Binary File:- A binary file contains raw binary data without any translation or any specific
encoding. In binary file, there is no delimiter for a line. As a result, binary files are faster and easier
for a program to read and write compared to text files. As long as the file dosen‟t need to be read by
people, binary files are the best way to store program information.
Basic Operations on File
1. Naming a File
2. Opening a File
3. Reading Data from File
4. Writing Data to File
5. Appending Data to File
6. Closing a File
File Processing
Using the above-mentioned basic operations, we can process files in many ways, such as;
• Creating a File
• Traversing a File for Displaying the Data on Screen
• Appending Data in File
• Inserting Data in File
• Deleting Data from File
• Create a Copy of File
• Updating Data in the File….etc.
Opening a File
The opening of a file in Python is done using open( ) function as per one of the following syntaxes:
a) file_object=open(<file_name/path>)
(CL-XII/CS/Assignment-4/2025-26/Page 1 of 5)
b) file_object=open(<file_name/path>,<mode>) For example, f=open(“sample.txt”)
Here, f is a file object which acts as a reference/link to the file “sample.txt”.
The file name can include description of the path if in case, it does not belong to the present working
directory (known as absolute path). Most of the cases, the file we are working on, belong to the current
directory. So, no need to specify the path (known as Absolute Path).
The second argument <mode> is optional. It states that what type of operation is going to be
performed on the file we opened. By default, all the files get opened in read mode (“r”).
The following table will clear your idea about different file modes supported by Python:
Text File Binary File Description Notes
Mode Mode
“r” “rb” Read Opens a file for reading only. File must exist already or Python will
raise an error.
“w” “wb” Write Opens a file for writing only. Overwrites the file if the file
exists. If the file does not exist, creates a new file for writing.
“a” “ab” Append Opens a file for appending. The file pointer is placed at the end
of the file if the file exists. So file’s existing data is
retained. That is, the file is in the append mode. If the file does not
exist, it creates a new file for writing.
“r+” “rb+” or Read Opens a file for both reading and writing. File must exist already or
“r+b” Write Python will raise an error.
“w+” “wb+” or Write Opens a file for both writing and reading. Overwrites the
“w+b” Read file if the file exists. If the file does not exist, creates a new file for
writing.
“a+” “ab+” or Append Opens a file for both appending and reading. The file pointer is
“a+b” Read placed at the end of the file if the file exists. File’s existing data
is retained. If the file does not exist, creates a new file for
writing.
Closing a File
An opened file is closed by calling the close( ) method of its file-object. In Python, files are
automatically closed once we finish working on it. But it is a good practice of closing your files
explicitly. This method will free up all the system resources used by the file. It means once the file
gets closed, its file object is not able to perform any more operation on that file.
Example: f.close( )
Following are the methods to read data from a file:
1. read()
2. readline()
3. readlines()
read() Method
The read() method is used to read the complete file. It returnsa string.
The syntax is:
(CL-XII/CS/Assignment-4/2025-26/Page 2 of 5)
file_object.read()
Example: Output:
read(n) Method
This method can read at most n bytes from the file. It also returns a string.
Example: Output:
f.read(1) read a single byte/character from file
readline() Method
readline() will return a line read, as a string from the file. First call to function will return first
line,second call next line and so on.
The syntax is:- file_object.readline()
Example:- Output:- Every time it will read a single line.
readlines() Method
readlines()can be used to read the entire content of the file. You need to be careful while using it
w.r.t.size of memory required before using the function. The method will return a list of strings, each
separated by \n.
The syntax is:- file_object.readlines()
(CL-XII/CS/Assignment-4/2025-26/Page 3 of 5)
Example:- Output:-
Writing to a File
Following are the methods to write data to a file:
1. write()
2. writelines()
write() Method
write() method takes a string (as parameter ) and writes it in the file. For storing data with end of line
character, you will have to add „\n‟ at the end of the line.
The syntax is:- file_object.write(string)
Example:- This will open the file test1.txt and write to it after removing its existing content.
If the file doesnot exist, then first it will create the file and then it will be written.
If the file mode is changed from „w‟ to „a‟, then the file will be opened in append mode. In append
mode, writing to a file will keep the existing content of the file remain same and after that new data
is added.
writelines Method
For writing a string at a time, we use write() method. It can't be used for writing a list, tuple etc.
into afile. Sequence data type can be written using writelines() method in the file. It's not that,
we can't write astring using writelines() method.
The syntax is: file_object.writelines(L)
It writes all strings in list L as lines to file referenced by
file_object.
Example:-
l=["Hello\n","Good Morning\n","How are you"]
f=open("sample.txt","w")
f.writelines(l)
f.close()
Important Programs on Text Files
Write a program/function in Python to:
(CL-XII/CS/Assignment-4/2025-26/Page 4 of 5)
1. a) Create a text file “DPS.TXT” containing the following data:
Welcome to DPS Durgapur. Our online classes are going on in full swing since the very beginning
of “Lockdown 1”.
We have already given two Online Exams.
b) Read back the entire file content-a) as a whole b) line by line, and display on screen.
c) Append more text of your choice in the file and display the file content as a list.
d) Display last line of file.
e) Display first line from 10th character onwards.
f) Read the first 20 characters.
2. A text file named “MATTER.TXT” contains some text, which needs to be displayed such that
Every next character is separated by symbol „#‟.
3. Write a method in Python to read lines from a text file “INDIA.TXT” and count the frequency
of the word “India”.
4. Write a program to count the total number of alphabets (both lower and upper case) spaces, lines
and characters in a given line of text from the above text file.
5. Write a program to count the lines which end with ‘n’ or ‘r’from any text file.
7. Write a program count the total number of lines and number of bytes (characters) in a textfile.
8. Write a function in Python to count the number of “Me” and “My” words present in a text file
“DIARY.TXT”.
9. Write a function in Python to count and display the number of lines starting with alphabet
“A”present in a text file “LINES.TXT”.
10. Write a program to display all the records of a text file along with line number.
***************************************
(CL-XII/CS/Assignment-4/2025-26/Page 5 of 5)