0% found this document useful (0 votes)
21 views2 pages

File Handling - Worksheet - Set 4 - QP

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)
21 views2 pages

File Handling - Worksheet - Set 4 - QP

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/ 2

FILE HANDLING – SET 4

Consider the file poem.txt which contains following data:


Why?
we work, we try to be better
we work with full zest
but, why is that we just don’t know nay letter
we still give our best
we have to steal
But, why is that we still don’t get a meal.

1.Write a program to read the complete file in a list.

2.Write a program to display size of the file in bytes.

3.Write a program to display number of lines in the file.

4.Write a program to count and display lines starting with letter ‘w’.

5.Write a program to count and display number of words in the file, also print length of
each word.

6.Write a program to count and display the occurrence of word ‘why’ in the file.

7.Write a program to count the words starting with vowel.

8.Write a program to display the longest line of the file.

9.What will be the output of the following code?


fh=file("poem.txt","r")
size=len(fh.read())
print(fh.read(5))
fh.close()

10.What will be the output of the following code?


fh=open("poem.txt","r")
s1=fh.read(28)
print(s1)
fh.close()

11. what will be the output of the following code?


fh=open("poem.txt","r")
s1=fh.readline()
s2=fh.readline()
s3=fh.read(15)
print(s3)
fh.close()

12.Write a program to copy the contents of the file ‘poem.txt’ to ‘poem1.txt’ barring the
lines starting with letter ‘b’.

13.Write a code to print just the last line of the file ‘poem.txt’.
14.Write a single loop to display all the contents of file poem.txt after removing leading
and trailing whitespaces.

15.A text file contains alphanumeric text say an.txt. write a program that reads this text
file and prints only the numbers or digits from the file.

16.Read the code given below and answer the question:


fh=open(“temp.txt”,”w”)
fh.write(“Bye”)
fh.close()
If the file contains “GOOD” before execution, what will be the contents of the file after
execution of this code?

17.What will be the output of the following code?


import pickle
names=['first','second','third','fourth','fifth']
lst=[]
for i in range(-1,-5,-1):
lst.append(names[i])
with open('text.dat','wb') as f:
pickle.dump(lst,f)
with open('text.dat','rb') as f:
nlist= pickle.load(f)
print(nlist)

18.Your recipe uses some ingredients. write a program to store the list of ingredients in a
binary file.

19.Write a program to increase the salary by Rs. 2000/- of the employee having empno
1251 in the file empl.dat.

20.Consider the following definition of dictionary Multiplex, write a function in python to


search and display all the contents in a pickled file cinema.dat, where Mtype key of the
dictionary is matching with the value ‘Comedy’.
Mutliplex={‘Mno’:……,’Mname’:………, ‘Mtype’:…….}

You might also like