COMPUTER
SCIENCE
Digital Notes for Grade XII
By
NASHRA JAVED
Computer Science,
Head of Department & PGT Teacher
BANGALORE INTERNATIONAL ACADEMY,
WHITEFIELD
Session 2024-2025
exponent, a complex result is delivered. For example, pow(-9, 0.5) returns a
value close to 3j.
FILE HANDLING
Files are of bytes stored on storage devices such as hard-disks. Files help in storing
information permanently on a computer.
Data Files:
Data files are files that store data pertaining to a specific application. The data files
can be stored in following ways:
• Text files: These files store information in the form of a stream of ASCII or UNICODE
characters. Each line is terminated by an EOL character. Python programs, contents written
in text editors are some of the example of text files.
• Binary files: These files store information in the form of a stream of bytes. No EOL character
is used and binary files hold information in the same format in which it is held in the memory.
The .exe files, mp3 file, image files, word documents are some of the examples of binary
[Link] can’t read a binary file using a text editor.
Difference Between Text File And Binary File
Text File Binary File
Stores information in ASCII characters. Stores information in the same format which
the information is held in memory.
Less prone to get corrupt as change reflects as Can easily get corrupted, corrupt on even single
soon as made and can be undone. bit change.
Store only plain text in a file. Can store different types of data.
Widely used file format and can be opened in Developed for an application and can be
any text editor. opened in that only.
Slower than binary files. Binary files are faster and easier for a
program to read and write the text files.
Opened in any text editor. Mostly .txt and .rtf Can have any application defined extensions.
are used as extensions of text files.
In Python, File Handling consists of following three steps:
• Open the file.
• Process file [Link] read or write operation.
• Close the file.
72
Opening Files :
To perform file operation, it must be opened first then after reading, writing,editing
operation can be [Link] create any new file then too it must be opened. On opening
of any file, a file relevant structure is created in memory as well as memory space is created
to store contents. Once we are done working with the file, we should close the file.
Itisdoneusing open() function as per one of the following syntax:
<fileobjectname>=open(<filename>)
<fileobjectname>=open(<filename>,<mode>)
For example:
stu=open("[Link]")
The above statement open file"[Link]" in file mode as read mode(defaultmode) and attaches it to
file object namely stu.
Consider one more file open statement: - stu=open
("e:\\main\\[Link]","w")
The above statement open file"[Link]"(storedinfoldere:\main)in write mode(because
of"w"givenasmode)and attaches it to file object namely stu.
• Please note that when you open a file in read mode,the given file must exist in folder,
otherwise Python will raise error.
File Object/File Handle:-A file object is a reference to a file on [Link] opens and makes it
available for a number of different tasks.
FileAccessModes:-
Text Binary
Description
file File
mode Mode
‘r’ ‘rb’ Read-Default value. Opens a file for reading, error if the file
does not exist.
‘w’ ‘wb’ Write- Opens a file for writing, creates the file if it does not
exist
‘a’ ‘ab’ Append- Opens a file for appending, creates the file if it does
not exist
‘r+’ ‘rb+’ Read and Write- File must exist, otherwise error is raised.
73
‘w+’ ‘wb+’ Read and Write-File is created if does not exist.
‘a+’ ‘ab+’ Read and write – Append new data
A file-mode governs the type of operations(e.g.,read/write/append) possible in the opened file i.e., it
refers to how the file will be used once it's opened.
Closing Files :
One must always close a file when the file is no longer required for any more
[Link] file can be closed by using the close( ) function which can be used w ith the
file pointer as follows:
<file_handle>.close()
Working with Text Files:
1. Read the data from a file
2. Write the data to a file
3. Append the data to a file
Reading from Text Files
Python provides three types of read functions to read from a data file.
• [Link]([n]) : reads and return n bytes, if n is not specified it reads entire file.
• [Link]([n]) : reads a line of input. If n is specified reads at most n bytes. Read
bytes in the form of string ending with line character or blank string if no more bytes are left
for reading.
• [Link](): reads all lines and returns them in a list
Examples:
Let a text file “[Link]” has the following text:
“Python is interactive language. It is case sensitive language.
It makes the difference between uppercase and lowercase letters. It is official language of
google.”
OUTPUT:
Example-1:Programusingread()function:
fin=open("D:\\pythonprograms\\Boo Python is interactive language. It is case sensitive
[Link]",'r') language.
str=[Link]( ) It makes the difference between uppercase and
74