0% found this document useful (0 votes)
2 views

81ebe (2)

The document provides guidelines for online class participation, emphasizing the importance of muting microphones and using chat for questions. It also covers Python programming concepts related to handling delimited files, including reading, writing, and modifying records using the os module and functions like flush(), rename(), and remove(). Additionally, it includes code examples for inserting, replacing, and deleting records in files.

Uploaded by

palashtiwari07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

81ebe (2)

The document provides guidelines for online class participation, emphasizing the importance of muting microphones and using chat for questions. It also covers Python programming concepts related to handling delimited files, including reading, writing, and modifying records using the os module and functions like flush(), rename(), and remove(). Additionally, it includes code examples for inserting, replacing, and deleting records in files.

Uploaded by

palashtiwari07
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

In order to ensure the success

of online class
• Keep your microphone and video on mute so that
there are no disturbances
• Please use chat to ask queries/ answer questions
when prompted
• As far as possible try to use laptop/pc
• The students must leave the group once the session
is over. Please do not use this platform for personal
chat/groups
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
Handling delimited files using readlines()
Task: To use the same file and find the student having highest
marks f=open("Marks.txt",'r')
a=f.readlines()
a=a[1:]
print(a)
high=0
for i in a:
i=i.rstrip('\n')
i=i.split(',')
print(i)
marks=int(i[2])
print(marks)
if (marks>high): #Type error: '>' not supported between instances of 'int' and 'str'
high=marks
name=i[1]
print(name,high)
f.close()

DEEPSHIKHA SETHI ---XII COMPUTER SCIENCE ----AIS MAYUR VIHAR


Recap
Handling Delimited files
Random access functions – seek() and tell()
Os module functions
os.getcwd()
os.path.abspath(filename)
os.path.isfile(filename)

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


The flush( ) function
• Anything written using write() is held in input buffer
• Python pushes it to the actual file later
• flush() forces the contents to the actual file on the storage device
• close() method allows Python to automatically flush the contents

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


The flush( ) function
f=open('out.txt','w+')
f.write('The output is \n')
f.write("My" + "work-status"+"is")
With this statement, the strings written so far,
f.flush() i.e., "The output is" and "My work-status is“
have been pushed on to actual file on disk.
s='OK'
f.write(s)
f.write('Finally Over\n')
The flush function ensures that whatever is
f.flush() held in Output buffer, is written on to the actual
file on disk
f.close()
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
OS Module
Files are always stored in current folder / directory by default. The os (Operating
System) module of python provides various methods to work with file and folder /
directories. For using these functions, we have to import os module in our program.
Some of the useful methods, which can be used with files in os module are as
follows:
1. os.getcwd() to know the name of current working directory

2. os.path.abspath(filename) will give us the complete path name of the data file.

3. os.path.isfile(filename) will check, whether the file exists or not.

4. os.remove(<filename>) will delete the file. Here filename has to be the complete
path of file.

5. os.rename(“Old file name”,”New file name”) will change the name of filename1
with filename2.
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
Usage of Rename() and Remove () Method

•The functions rename( ) and remove() play an important role in file


manipulation.

•Using these functions, you can insert a new record at desired


position in a file

• you can even delete and modify records.

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Usage of Rename() and Remove () Method

Insert 3rd record with the new record

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Steps to insert a new record at
appropriate position…..
1. Get the appropriate position. Either you should be given the position of new
record or you need to determine it yourself by applying some logic.
2. Copy the records prior to determined position to a temporary file say temp.txt
3. Append the new record in the temporary file temp.dat. (opened in append mode
‘a’)
4. Now append the rest of the records in temporary file temp.dat
5. Delete file marks.txtby issuing command.
remove(“marks.txt”)
6. Rename file temp.txt as marks.txt as follows:
rename(“temp.txt, “marks.txt”)

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


import os
def insertrecord(n): if count==n:
infile=open("Marks.txt","r") outfile.write(nrec)
outfile.write(rec)
outfile=open("temp.txt","w") else:
nrollno=int(input("Enter new rollno")) outfile.write(rec)
infile.close()
nname=input("Enter name") outfile.close()
nmarks=int(input("Enter new marks")) os.remove("Marks.txt")
os.rename("temp.txt","Marks.txt")
nrec=str(nrollno) +","+nname+","+str(nmarks)+'\n'
print ("New record successfully added.")
count=0
rec=" " # main Function
N=int(input("Enter which record you want to
while rec: insert"))
rec=infile.readline() insertrecord(N)

count=count+1

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Insert Record
Original File Temp File before renaming Marks file after renaming

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Steps to replace/modify a
record at appropriate
position…..
1. Get the appropriate position. Either you should be given the position of new record
or you need to determine it yourself by applying some logic.
2. Take the values of new record from the user.
3. Copy the records prior to determined position to a temporary file say
newstudent.txt. Repeat this step until you reach to your desired location.
4. After reaching your desired location, Append the new record in the temporary file
newstudent.txt(opened in append mode ‘a’)
5. Now append in the temporary file from the next record in newstudent.txt
6. Delete file student.txt by issuing command.
remove(“student.txt”)
6. Rename file temp.dat as stu.dat as follows:
rename(“newstudent.txt”, “student.txt”)
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
import os
def replacerecord(n):
print("Enter New Record:") os.remove("student.txt")
nrollno=int(input("Enter new rollno")) os.rename("newstudent.txt","student.txt")
nname=input("Enter name")
nmarks=int(input("Enter new marks"))
newrec=str(nrollno) +","+nname+","+str(nmarks)+'\n' replacerecord(2) # main Function
newf=open("student.txt","r")
oldf=open("student.txt")
newf=open("newstudent.txt","w")
print(newf.read())
i=1 newf.close()
rec=oldf.readline()
while(rec):
if(i==n):
newf.write(newrec)
else:
newf.write(rec) Usage of
rec=oldf.readline()
i+=1
Rename() and
oldf.close() Remove () Method
newf.close()

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Modify/Replace Record

OUTPUT After Replacing Record


Before Replacing Record
Enter New Record:

Enter new rollno40

Enter nameDeepak

Enter new marks100


23,Amogh,89
40,Deepak,100
25,Harsh,76
26,Girish,67

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Usage of Rename() and Remove
() Method
Alternative
method rec.insert(n-1,newrec)
def replacerecord(n): newf.writelines(rec)
newrec=input('Enter the new record') oldf.close()
newf.close()
newrec+='\n'
os.remove("student.txt")
oldf=open("student.txt") os.rename("newstudent.txt","student
newf=open("newstudent.txt","w")
rec=oldf.readlines() replacerecord(2)
print(rec) newf=open("student.txt","r")
rec.pop(n-1) newf.read()
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
Delete Record
1. Get the appropriate position. Either you should be given the position of new
record or you need to determine it yourself by applying some logic.
2. Copy the records prior to determined position to a temporary file say marks.txt.
Repeat this step until you reach to your desired location.
3. After reaching your desired location, just write pass statement i.e. skip the writing
into temporary file.
4. Now append in the temporary file from the next record in Marks.txt
5. Delete file Marks.txt by issuing command.
remove(“marks.txt”)
6. Rename file temp.dat as stu.dat as follows:
rename(“temp.txt”, “marks.txt”)
DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR
import os
def Deleterecord(n): infile.close()
infile=open("Marks.txt","r") outfile.close()
os.remove("Marks.txt")
outfile=open("temp.txt","w") os.rename("temp.txt","Marks.txt")
count=0 print ("Record deleted successfully.")
rec=" "
while rec: N=int(input("Enter which record you want to Delete"))
Deleterecord(N)
rec=infile.readline()
count=count+1
if count==n:
pass
else:
outfile.write(rec)

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


Delete Record
Before deleting the 2nd record After deleting the 2nd record

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR


H.W
Write a program to delete the third record from a file “Result.txt”.
Records are separated by lines as shown below:

12 Hazel 67.75
15 Jiya 78.5
16 Noor 68.9
17 Akshra 78.9
18 Naksh 78.0

DEEPSHIKHA SETHI ----XII COMPUTER SCIENCE ---- AIS MAYUR VIHAR

You might also like