0% found this document useful (0 votes)
0 views12 pages

File Handling - Text File Notes & Programs

The document provides a comprehensive overview of file handling in Python, detailing the importance of persistent data storage and the various types of files that can be managed, including text, binary, and CSV files. It explains the process of file I/O, including opening, reading, writing, and closing files, as well as the use of different file modes and methods for manipulating file content. Additionally, it includes several example programs demonstrating common file operations such as counting characters, words, and lines in text files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views12 pages

File Handling - Text File Notes & Programs

The document provides a comprehensive overview of file handling in Python, detailing the importance of persistent data storage and the various types of files that can be managed, including text, binary, and CSV files. It explains the process of file I/O, including opening, reading, writing, and closing files, as well as the use of different file modes and methods for manipulating file content. Additionally, it includes several example programs demonstrating common file operations such as counting characters, words, and lines in text files.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

File Handling in Python

• We have seen yet only the transient programs. The programs which run for a short period of time
and give some output and after that their data is disappeared. And when we again run those programs
then we have to use new data.
• This is because the data is entered in primary memory which is temporary memory and its data is
volatile.
• Those programs which are persistent i.e. they are always in running or run for a long time then their
data is stored in permanent storage (e.g. harddisk) . If the program is closed or restarted then the data
used will be retrieved.
• For this purpose the program should have the capability to read or write the text files or data files.
These files can be saved in permanent storage.
• The meaning of File I/O (input-output) is to transfer the data from Primary memory to secondary
memory and vice-versa

Why the Files are used?


• The data stored with in a file is known as persistent data because this data is permanently stored in
the system.
• Python provides reading and writing capability of data files.
• We save the data in the files for further use.
• As you save your data in files using word, excel etc. same thing we can do with python.
• “A File is a collection of characters in which we can perform read and write functions. And also we
can save it in secondary storage.”

Data File- A file is a sequence of bytes on the disk/permanent storage where a group of related data
is stored.
File handling in Python enables us to create, update, read, and delete the files stored on the file
system through our python program.Data File handling takes place in the following order:
➢ Open the file.
➢ Process file i.e perform read or write operation.
➢ Close the file

Types of files in Python:


Python allows us to create and manage three types of data files.
1- Text file
2- Binary file
3- CSV file

Text file: A text file is simply a sequence of ASCII or Unicode characters. A line is a
sequence of characters, stored on permanent storage. In a text file, each line is terminated
by a special character, known as End of Line (EOL). This EOL character is ‘\n’ or ‘\r’ or
combination of both Text file can be created using any text editor. Ex. Myfile.txt.
Binary file: A binary file stores the data in the same way as stored in the memory. The .exe files,
mp3 file, image files, word documents are some of the examples of binary files. We can’t read a
binary file using a text editor.

CSV file:
1) CSV is a simple file format used to store tabular data, such as a spreadsheet or database.
2) CSV stands for "comma-separated values“.
3) A comma-separated values file is a delimited text file that uses a comma to separate values.
4) Each line of the file is a data record. Each record consists of one or more fields, separated by
commas. The use of the comma as a field separator is the source of the name for this file format.

Absolute Path – It is a full path of the file from the root directory.
Ex : - C:\\Users\\Tanmay\\Desktop\\Delete\\file_handling.txt
f=open(“C:\\Users\\Tanmay\\Desktop\\Delete\\file_handling.txt”,r)

Relative Path –It is the path of the file from the current working directory.
Relative Path is the hierarchical path that locates a file or folder on a file system
starting from the current working directory.
f=open(“file_handling.txt”, r)

TEXT FILE
Opening a Text File
A file can be opened for – read, write or append data.
 Use the open() function to open a text file.
 Syntax:
file_object = open("filename.txt", mode)
Note: Default opening mode is read
 Replace "filename.txt" with the name of the text file and mode with the desired file open mode.
Examples-
FILE MODE
It defines how the file will be accessed.
File accessing modes
> read( r ) : To read a file
> write(w) : To write into a file
> append( a) : To write at the end of the file

CLOSING FILES

close() : the close() method of a file object flushes any unwritten information and close the file object
after which no more writing can be done.
SYNTAX: fileobject.close()

Use of With Clause


with statement: with statement will automatically close the file after the nested blockof code. It is
guaranteed to close the file no matter how the nested block exits. Even if an exception occurs with
statement will handle it and close the file. It is used in the following manner:
with open(<filename>,<filemode>) as <filehandle>:
<file manipulation statements>
Syntax:
with open(“file name”,access mode) as file object
example with open(“book.txt”,’r’) as f

Reading data from Text file


read()-reads and returns the entire data stored in the file, starting from current cursor position up to
the end of the file.It returns the file's content as a string.
Code Example:
with open("example.txt", "r") as file:
content = file.read()
print(content)
read(n): reads and returns n number of characters from the file starting from current cursor position. The
returned data forms one string.
b) Reading a Single Line
To read a single line from a text file, you can use the `readline()` method. It reads and returns
a single line from the file.
Example Code :
with open("example.txt", "r") as file:
line = file.readline()
print(line)
c) Reading All Lines
To read all lines of a text file into a list, you can use the `readlines()` method. Each line is
stored as an element in the list.
Example Code :
with open("example.txt", "r") as file:
lines = file.readlines()
for line in lines:
print(line)
Each time read/write operation is performed two things happen
o read/write operation is performed at the current cursor position in the file.
o File pointer moves forward by the specified number of bytes.
 While reading if no data is left in the file, then a blank string is returned.

Writing/Appending Data to a Text File


 Use the write() method to write data to a file.
The write() function will write the content in the file without adding any extra characters.
file_name.write(content)
 Use the writelines() method to write a sequence of lines to a file.
file_name.writelines(sequence_of_lines)
 If the file is opened in write mode ('w' or 'w+'), it will overwrite existing content.
 If the file is opened in append mode ('a' or 'a+'), new data will be added to the end of the file.

SETTING OFFSETS IN A FILE:


To access the data in random fashion then we use seek () and tell () Method

seek() method is used to position the file object at a particular position in a file. The syntax of
seek() is:
file_object.seek(offset [, reference_point])
EX:
file = open("example.txt", "r")
data = file.read(100)
file.seek(0) #Move the file pointer to the beginning of the file
next_data = file.read(50)
file.close()

In the above syntax, offset is the number of bytes by which the file object is to be moved.
reference_point indicates the starting position of the file object. That is, with reference to which
position, the offset has to be counted. It can have any of the following values:0 - beginning of the file
1 - current position of the file
2 - end of file
By default, the value of reference_point is 0, i.e. the offset is counted from the beginning of the file.
For example, the statement fileObject.seek(5,0) will position the file object at 5th byte position
from the beginning of the file.

tell() method returns the current file position. This function returns an integer that specifies the
current position of the file object in the file. The position so specified is the byte position from the
beginning of the file till the current position of the file object.
The syntax of using tell() is:
file_object.tell()
Example:
# Create and write to a file
with open("example.txt", "w") as f:
f.write("Hello World")
# Read and check pointer position
with open("example.txt", "r") as f:
print("Initial pointer position:", f.tell()) # usually 0
content = f.read(5)
print("Content read:", content)
print("Pointer position after reading 5 characters:", f.tell())

O/P
Initial pointer position: 0
Content read: Hello
Pointer position after reading 5 characters: 5

Important Programs related to Text File


program 1: Read data from text file using read()
f=open("sample.txt")
data=f.read() # read entire data starting from 1st character
print("first read::",data)
data=f.read(3) # Will return a blank list as End Of File has reached because of first read stmt
print("second read::",data)
data=f.read(5) # Will return a blank list as End Of File has already reached
print("third read::", data)
f.close()

program 2: Read data from text file using readlines()


#read all the lines from file in the form of list of lists where each list represents a line in the file
f=open("school.txt")
data=f.readlines()
print(data)
f.close()

program 3: display data using file object


f=open("school.txt")
for i in f:
print(i)
f.close()

Progarm 4: DISPLAY TOTAL NO OF CHARACTERS, WORDS

f=open("sample.txt" , "r")
d=f.read()
c=len(d)
str=d.split() # to convert a string into list of strings
w=len(str)
print("No of characters= ",c)
print("No of words= ",w)
f.close()
Program4: A pre-existing file info.txt has some text written in it.Write a python function
countvowel() that reads the contents of the file and counts the occurrence of vowels
(A,E,I,O,U,a,e,i,o,u) in the file.

def countvowel():
fp=open("info.txt","r")
data=fp.read()
count=0
for x in data:
if x in 'AEIOUaeiou':
count=count+1
print("Total no. of vowels",count)
fp.close()
#main
countvowel()

Program 5: Write a function COUNT_AND () in python to read the textfile “story.txt” and
count the number of times “AND” occurs in file ,(include AND/and/And in the counting)
#COUNT AND DISPLAY TOTAL NO OF OCCURENCE OF WORD 'and'
def COUNT_AND():
file=open("story.txt" , "r")
line=file.read()
word=line.split()
count=0
for x in word:
if x in ["AND","and","And"]:
count=count+1
print("No. of Occurence of word and:-",count)
file.close()
#main
COUNT_AND()

Program 6: Write a function DISPLAYWORDS() in python to display the count of words


starting with ‘t’ or “T” in a text file “story.txt”
#TO COUNT TOTAL NUMBER OF WORDS STARTING WITH 'T' or 't'

file=open("story.txt" , "r")
d=file.read()
str=d.split()
count=0
for word in str:
if word[0]=='T' or word[0]=='t':
count=count+1
print("Total number of words starting with T or t : ",count)
file.close()
Program 7: Write a function count_Dwords ( ) in python to count the words ending with a digit
in a text file “Details.txt”.
Example:
If the file content is as follows:

On Seat2 VIP1 will sit and


On Seat1 VIP2 will be sitting.

Output will be:


Number of words ending with digit are:4

def count_Dwords():
fp= open("Details.txt","r")
line=fp.read()
word=line.split()
count=0
for x in word:
if x[-1] in "0123456789":
count=count+1
print("Number of words ending with digit are:",count)
fp.close()
#main
count_Dwords()

Program 8: A pre-existing text file data.txt has some words written in it.Write a python
program function displaywords() that will print all the words that are having length greater
than 3.
Example:
For the file content:
A man always want to strive higher in his life.
He wants to be perfect.

The output after execution will be:


Always want strive higher life wants perfect
Total words= 7

def displaywords():
fp=open("data.txt","r")
line=fp.read()
word=line.split()
count=0
for i in word:
if len(
i)>3:
print(i,end=" ")
count=count+1
print("\ntotal no of words",count)
fp.close()
#main
displaywords()
Program 9: Write the definition of a function count_Line( ) in python, which should read each line of
a text file “Shivaji.txt” and count total number of lines present in text file. For example if the content
of text file is:
Shivaji Maharaj was a great Maratha warrior and the founder of the Maratha Empire in western India.
He was known for his brilliant military tactics, administrative skills, and respect for all religions.
Shivaji's courage and leadership laid the foundation for Indian resistance against Mughal rule.

def count_line():
fp=open("Shivaji.txt","r")
lines=fp.readlines()
totalline=len(lines)
print("total number of lines",totalline)
fp.close()
count_line()

Or 2nd way to write the above program:


def count_lines():
fp=open("Shivaji.txt","r")
count=0
for x in fp:
count=count+1
print("total number of lines=",count)
fp.close()
count_lines()

Program 10: Write a python function named Longlines() which read the content of text file “lines.txt”
and display those lines from the file which have atleast 10 words in it.

def Longlines():
fp=open("lines.txt","r")
for line in fp:
lst=line.split()
if len(lst)>=10:
print(line,end=" ")
fp.close()
Longlines()

Program 11: Write a function in python to read a text file ,Alpha.txt and display those lines which
begin with the word ‘You’.

def print_lines():
fp=open(“Alpha.txt”,”r”)
for x in fp:
lst = x.split()
If lst[0]==”You”:
print(x,end=” “)
fp.close()
#main
Print_lines()
Program 12: Write a function ETcount( ) in python ,which should read each character of a text file
“Testfile.txt” and then count and display the count of occurrence of alphabets E and T indivisually
(including small cases e & t too)
Example:
Today is a pleasant day.
It might rain today.
It is mentioned on the weather sites.

The ETcount() function should display the output as:


The number of E or e :6
The number of T or t :9

def ETcount():
fp=open(“Testfile.txt”,”r”)
countE=0
countT=0
for line in fb:
for ch in line:
if ch in “Ee”:
countE=countE+1
if ch in “Tt”:
countT=countT+1
print(“The number of E or e:”,countE)
print(“The number of T or t:”,countT)
#main
ETcount()

Program 12: Write a method countlines() in python that read each line of “testfile.txt” and display
those lines which are not starting with any vowel.
def countnotvowel():
fp=open("testfile.txt","r")
data=fp.readlines()
count=0
for x in data:
if x[0] not in 'AEIOUaeiou':
count=count+1
print("Total no. of lines not starting with vowels",count)
fp.close()
#main
countnotvowel()

Program13: A text file data.txt has some words written in it . Write a python function
countwords() that counts and prints the number of words in each line that has length of more
than 4 characters.
EX:
If file content is:
A man always wants to strive
Higher in his life
He wants to be perfect
The output should be:
Line 1 - 3 words
Line 2 - 1 word
Line 3 - 2 Words

def countwords():
fp=open("data.txt","r")
lines=fp.readlines()
lineno=1 #Initial line no
for line in lines:
words=line.split()
count=0
for word in words:
if len(word)>4:
count+=1
print("line",lineno,"-",count,"words")
lineno +=1
fp.close()
countwords()

Program 14 Write a function Disp() that display all those words from a text file that ends with word
‘at’ from the text file “create.txt”

def disp():
fp=open(“create.txt”,”r”)
for line in fp:
words=line.split()
For x in words:
if x.endswith(“at”):
print(word)
Fp.close()
disp()

Program 15: Write a python function countT() which read the text file lines.txt and display those
lines which have minimum 2 words starting with T ot t.

def countT():
fp=open(“lines.txt”,”r”)
for line in fp:
words=line.split()
count=0
for word in words:
if word.lower().startswith(‘t’):
count +=1
if count>=2:
print(line)
fp.close()
#main
countT()

Program16: Write a python program to display the words containing @cmail from a text file
“Email.txt”.

def show():
f=open("Email.txt",'r')
data=f.read()
words=data.split()
for word in words:
if '@cmail' in word:
print(word)
f.close()
show()
Write in TEXT File

Progarm 1: Using \n to separate data


f=open("sample.txt" , "w")
f.write("INDIA\n")
f.write("NEW DELHI\n")
f.write("end")
print("Data written to file")
f.close()

Progarm 2 : Accepting data from user


f=open("sample.txt" , "w")
text=input("Enter Subject name:")
f.write(text)
f.write("\n")
mm=80
f.write(str(mm)) #numeric data is converted to string
f.close()

Progarm 3: Opening file in append mode and enter some data to it.
n=input("enter name of file") # Accept file name from the user
f=open(n , "a")
for i in range(3):
t=input("enter name:")
f.write(t)
f.write("\n")
f.close()
f=open("sample.txt","r")
print(f.read())
f.close()
>>>write() function cannot be used for writing sequence i.e. list, tuple etc
>>> writelines() is used to write sequence of strings to a file

Program 4: Writing data using writelines ()


f=open("sample.txt","w")
l=["neem " , "tulsi " , "mint "]
f.writelines(l)
f.close()
f=open("sample.txt","r")
print(f.read())
f.close()
Progarm 5: CREATE A FILE BY COPYING THE CONTENTS OF FILE SCHOOL.TXT TO
BACKUP.TXT
f1=open("school.txt")
f2=open("backup.txt", 'w')
d=f1.readlines()
f2.writelines(d)
f1.close()
f2.close()

Progarm 6: Delete all the lines from the file sample.txt which are not starting with 'H' or 'I'
import os
f1=open("sample.txt")
f2=open("backup.txt", 'w')
d=f1.readlines()
for l in d:
if l[0]=='H' or l[0]=='I':
f2.writelines(l)
f1.close()
f2.close()
os.remove("sample.txt") #remove file sample.txt
os.rename("backup.txt","sample.txt") # rename backup.txt to sample.txt
f=open("sample.txt","r")
print(f.read())
f.close(

Program 7: Reverse the Contents of a File Line by Line


with open("sample.txt", "r") as f:
lines = f.readlines()
with open("reversed.txt", "w") as f:
for line in reversed(lines):
f.write(line)

You might also like