You are on page 1of 5

File Handling chapter notes

Class- XII , computer science

Introduction
As we know, Files are an essential part of computers. Everything
stored in the computer is saved in files. These files are a collection of
bytes stored in computers. Data can be represented in specific forms
in files. These files can be accessed at any time. These files are saved
with an extension or file types. They need a specific program to read
data and write data.

For Ex. Documents can be opened and read through Word Processing
software such as MS Word, OO Writer, Word Star, etc.

File handling is the process of handling data by software including IO


operations. These files stored in a directory on a hard drive. Whenever
any operation is carried out, the file is opened and make available to
write upon on reading purposes on RAM.

Need for a data file


The computer has a powerful feature of saving data. As you know data
means raw facts and figures. and meaningful data is known as
information. These data can be stored in a file. Whenever users need
such data, data can be presented by files.
When you develop a program in python, the final output of the
program is not available for future use. Data files serve this purpose.
They can store the output in the following files:

1. Text Files: It has an extension .txt. It can be directly opened by a


text editor like notepad, Word pad, etc. It is capable to handle textual
data and information. The text files contain a series of lines. A line is a
set of characters or strings. These characters can be in ASCII or
UNICODE form. In the text files, each line is terminated by ‘EOL-End
Of Line’ i.e ‘/n’ in python.
2. Binary Files: Binary files are capable to store large files such as
images, videos, audio files, etc. These files have a specific pattern to
read data in the correct type. These files don’t have any delimiter.
They are easier and faster than text files. In binary files data
interpreted by correct data type. Python provides specific functions to
handles these data types and data files.
3. CSV (Comma Separated Values) Files: It is a plain text file that
contains a list of data. CSV files can be opened and operated by MS
Excel and allow to export and import data. It can handle big data. All
the values are separated by a comma.

Data file operations


The following tasks will be performed on data files.

1. Creation of files
2. Opening files
3. Reading files
4. Writing files
5. Appending data in files
6. Deleting Data from files
7. Creating copy
8. Updating files data

Text Files:
Open a text file:
Syntax:<file object> = open(file_name,access_mode)
file object : It is just like a variable or object
open(): It is a function with two parameters.

1. file_name: It accepts a file name with .txt extension.


2. access_mode: It specifies the mode to access the file. The default
mode is reading mode. These modes are a) r: to read a file b) w:
to write c) a: append contents

Note:
1. ‘+’ sign is used to open the file for both modes reading and writing
after access_mode. When it is used the file pointer will be at the
beginning of the file
2. ‘b’ along with above modes to work with binary files
Example:
f = open(“MyFile.txt”,”r”) – Open file for reading
f = open(“MyFile.txt”,”w”) – Open file for writing

Reading file:
Python provides the following functions for reading files:
1) read(): To read the whole file, take a look in the following
example.

Reading text file and displaying its contents in output


2) read(n): It read n number of characters from the beginning of the
file. If the file contains less than n number of characters than it will
read file up to EOF (End Of File).

Reading n no. of characters


3) readline(): It read a line from starting the place of cursor where
its placed up to EOL (End of Line).

Using readline() function in python


4) Reading entire file using readline():

Reading whole file using readline() function


5) readlines(): To read specific lines from text file

ing whole file using readlines() function


6) Reading specific lines:

Reading a specific line from the text file


When a variable is assigned with fileobject.readlines() it stores no. of
lines in a list with starting index 0(zero).
Writing to File
The following functions are used to writing into files.
1. write(): It takes a string as a parameter to write in the file. ‘n’ is
used to specify the end of the string.
n
2) writelines(): This method is used to write a sequence of lines,
strings, tuples, etc in a file.

Writing in text file using writelines() function


Four lines created in a list l and written in a text file using writelines()
function.

You might also like