62 BDD
62 BDD
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
Learning Objectives
Introduction of rstrip(), lstrip() and strip() methods
Random access functions-seek() and tell()
File object attributes: closed, name, mode
a) Read back the entire file content using read( ) or readlines( ) and display on the screen.
b) Append more text of your choice in the file and display the content of file with line numbers prefixed to
line.
c) Display last line of file.
d) Display first line from 10th character onwards.
e) Read and display a line from the file. Ask user to provide the line number to be read.
f) Find the frequency of words beginning with every letter i.e.
(for the above example)
Words beginning with a: 5
Words beginning with n: 2
Words beginning with p: 2
Words beginning with o: 5 and so on
Output
12
----------------
11
----------------
4
Line = fin.readline().rstrip(‘\n’)
Record=line.split(‘~’)
Output:
Record = [‘12’, ‘34.5’, ’64.75’, ‘55.50’]
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()
Reference position
seek() No of bytes
to be moved from where the byte
has to be moved
Fileobject.seek(offset,whence)
0 means beginning
1 means current
2 means end
tell()
Fileobject.tell()
12
rsatile
• File.mode
Returns access mode with which file was opened.
f=open('fruits.txt','r') False
print(f.closed) fruits.txt
print(f.name) r
print(f.mode)
2. os.path.abspath(filename) will give us the complete path name of the data file.
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