You are on page 1of 9

INSPIREWEBSOFT

A UNIT OF MEET

AN ISO 9001:2015 CERTIFIED

COMPUTER SCIENCE

TEXT FILE HANDLING


IN
PYTHON
CLASS XII CBSE

BY AVLOKAN MOHAN
[MASTER OF COMPUTER APPLICATIONS]
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com
TEXT FILE
It consists of sequence of lines.
A line is a sequence of characters (ASCII or UNCODE), stored on permanent storage media.
Each line is terminated by a special character, known as EOL(End of Line), by default(‘\n’)
These files can be text file with extension .txt
It stores data in the form of text i.e. human readable form.

FILE MODES
The following modes can be used to work with the text files:-

Modes Description
r  It is used for reading only a file.
 It is the default mode.
 If file does not exist FileNotFoundError
 File pointer is placed at the beginning of the file.
w  It is used for writing only into a file.
 If file does not exist, it creates a new file.
 If file exists, then overwrites it.
a  It is used for appending or writing into an existing file at the end.
 If file does not exist, it creates a new file.
 File pointer is at the end of the file if the file exists.
r+  It is used for reading or writing both.
 File pointer will be at the beginning of the file.
w+  It is used for reading or writing both.
 If file does not exist, it creates a new file.
 If file exists, then overwrites it.
 File pointer will be at the beginning of the file.
a+  It is used for reading or writing both.
 File pointer will be at the end of the file.
 If file does not exist, it creates a new file.

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |1
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com
HOW TO PERFORM OPERATIONS ON TEXT FILE?
We will perform the following operations on the file.
1. Writing
2. Reading

To perform any kind of operations on the file, follow the steps :-


1. Open the file
Syntax: - <file_object> = open(“<file_name>”,”<mode>”);
2. Process the data
3. Close the file
Syntax: - <file_object>.close()

Providing file_name
1. Relative Path :- This is the path of the current folder where the file name is specified by default.
Example :- FileObject = open(“inspirewebsoft.txt”, “w”)

2. Absolute Path :- This is the path where the whole path is specified for creating and storing the file.
Example :- FileObject = open(“E:/Desktop/Documents/inspirewebsoft.txt”, “w”)

Drive Folder File

WRITING DATA ON TEXT FILE


To write data on the text file, follow the steps
1. Open the file
2. Write in the file(process the data)
3. Close the file

Writing using write() function


This function takes argument as a string.
For storing numeric value, we have to convert it to string.
For storing data with end of line character, we will have to add ‘\n’ character to the end of the string.

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |2
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com
If file will not exist, it will create
itself i.e. no error will occur.

Writing using writelines() function


This function can takes argument as a string, list of strings, tuple of strings, dictionary of strings(but it will store key
only in the file)

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |3
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com

READING DATA FROM A TEXT FILE


To read data from the text file, follow the steps
1. Open the file
2. Read data from the file(process the data)
3. Close the file

Reading using read() function


It reads the entire file content as a single string.
After reading the file, pointer goes to the end of the file.

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |4
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com

If file will not exist, it will give


FileNotFoundError

Reading using readline() function :-


It will read one line at a time. When EOL is reached, it will return empty string.
Delimiter is new line(‘\n’).
After reading the first line , pointer goes to the first character of the next line.

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |5
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com

Reading all lines using readline() function :-

Reading using readlines() function :-


It picks up the data line by line and stores it as an item of the list.
It reads the entire file content as a list of the string/strings.
The number of items in list will be based on the number of lines written in the text files.
After reading the file, pointer goes to the end of the file.

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |6
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com

Passing number of characters to read(), readline() and readlines() :-


There is no difference between read() and readline() when we pass parameter. They will perform the same task.

welcome\nhow are you\n


123456789………..

No Error

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |7
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798
BY AVLOKAN MOHAN #TEXT FILE HANDLING IN PYTHON
(MASTER OF COMPUTER APPLICATIONS)
INSPIREWEBSOFT CBSE-XII
(COMPUTER SCIENCE)
A UNIT OF MEET (AN ISO 9001:2015 CERTIFIED)
www.inspirewebsoft.com

It will take one line at a time in the form of list, i.e. it


will not give number of characters you need.

with STATEMENT
Instead of using open() or file() for creation of file, with statement can also be used for the same purpose.
We can use this statement to group file operation statements within block.
Using with ensures that all the resources allocated to the file objects get deallocated automatically once we stop using the
file.
We do not need to close the file explicitly using with statement.
syntax
with open(“<file_name>”, “<mode>”) as file_object:
file manipulation statements

APPENDING DATA IN TEXT FILE


Append means ‘to add to’.
To append data in the text file, follow the steps
1. Open the file
2. Write in the file(process the data) and use the access mode ‘a’
3. Close the file

IX,X(ICSE) XI,XII(ISC & CBSE) XI,XII(CBSE)


Computer Application Computer Science Informatics Practices
Page |8
FOR B.TECH / BCA / MCA – PROJECT BASED TRAINING
OTHER LANGUAGES:- C, C++, Core Java, Core Python, GUI Programming in Python, Python with web using Django,
Data Structure, HTML, CSS, JavaScript etc. Mob. No.-9990039798

You might also like