h files exam
h files exam
h files exam
1. Define a function SGcounter() that counts and display number of Sand Gpresent in a text file 'A.txt
eg., SAGAR JOON IS GOING TO MARKET.
It will display S:2 G:2
def Shctunten):
52/60
feohen ("At x+'");S- 0;(z0
R=f.nigd0
foh in R:
if .ujgoer)=='s'!
S+1
Ehint(1s:)s," Ga:"6)
4.cIoSeLin Python that counts the number of "is", "am" or "are" words present in a text file
2. Writeafunction
"HELLO.TXT". If the "HELLO.TXT" contents are as follows:
Phint("Numibon
f.closet)
of Ines slaling
5.Write a method in python toread lines from a text file AZAD.TXT and display those lines, which are
starting with an alphabet T'. 3x5=15
dof fun():
R=foneodlines()
if o]=:'T":
bhint («)
6Shreyas is a programmer, who has recently been given atask to write auser defined function named
write_bin()to create a binary file called Cust file.dat containing customer information customer
number (c_no), name (c_name), quantity (qty), price (price) and amount (amt) of each customer. 4
import pickle
def write bin () :
bin file #Statement 1
while True:
c no=int (input ("enter customer number"))
c name=input ("enter customer name")
qtyint (input ("enter qty"))
price=int (input ("enter price") )
if #Statement 2
print ("Quantity less than 10. .Cannot SAVE")
else:
amt-price * qty
c detail= (c no,c name, qty, price,amt]
#Statement 3
ans=input ("Do you wish to enter more records y/n")
if ans.lower () =='n':
#Statement 4
#Statement 5
#Statement 6
The functión accepts customer number, name, quantity and price. Thereafter, it displays the message
Quantity less than 10... Cannot SAVE', if quantity entered is less than 10. Otherwise the function
calculates amount as price quantity and then writes the record in the form of a list into the binaryfile.
i) Write the correct statement to open a file 'Cust_file.dat' for writing the data of the customer.
chen ("Custiledot""wb")
(i) Which statement should Shreyas fill in Statement 2to check whether quantity is less than 10.
aty<i0:
(ii)Which statement should Shreyas fill in Statement 3 to write data to the binary file and in
Statement 4 to stop further proçessing if the user does not wish to enter more records.
Stade mend 3: PiCKlR,dumb(C-detail, bin-file)
Stokment4: bneak
(iv) What should Shreyas fill in Statement 5 to close the binary file named Cust_file.dat and in
Statement 6 to call a function to write data in binary file?
StalemantS: bin-file.closeC)
Stotemetbi white_binl)
7.
b)Write the definition of a function SHOWHIGH () in Python,which reads the records of PLANTS.
dat and displays those records for which the PRICE is more than 500.
K] X
pitkbedung
f.flushl)
fclosel)
8 Suppose content of 'Myfile.txt' is: 1
Honesty is the best policy.
What willbe the output of the following code?
myfile =open("Myfile.txt")
x=myfile.read() olp’ 27
print(len(x)
myfile.close()
9 Suppose content of 'Myfile.txt' is 1
Culture is the widening of the mind and of the spirit.
What willbe the output of the following code?
myfile = open("Myfile.txt")
x= myfile.read()
y= x.count('the')
print(y) olp 3
myfile.close)
10 Raghav is trying to write a tuple tup1 =(1,2,3,4,5) on a binary file test.bin. Consider the following
code written by him. 1
import pickle
tupl = (1,2,3,4,5)
myfile = open("test.bin",'wb')
pickle. #Statement 1 myfile.close() dumytut,mgile)
Write the missing code in Statement 1
11.
Winte Sumer
Spriog
Tart Tart
13 Write a statement to send the file pointer position 10bytes forward from current location of file,
consider fp as file object. 1
15 Howmany lines does the file myfile00.txt has after the code is executed? 1
>>> mylist=['Iindia', \nis, \nmy', \ncountry, \nand', "\nl',\nam", \na',nproud', \ncitizen']
>>> spc=open("'myfile00.txt","w")
>>> spc.writelines(mylist)
a). 2b). 10c). 9 d). 1
16Write the definition of a function Change.Gender() in Python, which reads the contents of a text
file "BIOPIC. TXT" and displays the content of the file with every occurrence of the word 'he
replaced by 'she. For example, if the content of the file "BIOPIC. TXT" is as follows:
Lost time he went to Agra, there was too much crowd, which he did not like. So this time he decided to
visit some hill station.
The function should read the file content and display the output as follows:
Last time she went to Agra, there was too much crowd, which she did not like. So this time she
decided to visit some hillstation. 3
def ChangeGendeal0:
f: ohen("6IoPIG.TMT"+") ()
I.ubhen=='NE!
R:f. heod(split() x1.5 R='She'!
fon Kinr:
þmint (h,ende )
17
student.csv
phint()
c)Count the total number of students
te impot csv
dec cout 0:
t: obun("Studort. (sv "a") 1
phintlo
hoodeAz (5V. reoden r);c=-0, falocel)
hext(heoden)
Ct=|