File Handling || Type B || Sumita Arora || Class 12 || Computer science || Information practices || Solution || 




Q1 = How are following codes different from one another?
(a)
my_file = open('poem.txt', 'r')
my_file .read()
(b)
my_File = open('poem.txt", "r)
my_file .read(100)



Q2 = If the tile 'poemBTH.txt' contains the following poem (by Paramhansa Yogananda):
God made the Earth;
Man made confusing countries.
And their fancy-frozen boundaries.
But with unfound boundless Love
I behold the borderland of my India
Expanding into the world.
Hail, mother of religions, lotus, scenic beauty, and sages!
• Then what outputs will be posted by both the code fragments given in question 1.




Q3 = Consider the file 'poemBTH.txt' given above (in previous question). What output will be produced by following code fragment?
obj1 = open('poemBTH.txt', 'r')
s1 = obj1.readline()
s2.readline(10)
s3 = obj1.read(15)
print(s3)
print(obj1.readline())
obj1.close()



Q4 = Consider the file 'poemBTH.txt' and predict the output of following code fragments if the file has been opened in filepointer file1 with code:
file1 = open (“E\\mydata\\poemBTH.txt”, ‘r+’)
(a)
print ("A. Output 1")
print (file1.read())
print ()
(b)
print ("B. Output 2")
print (file1.readline())
print ()
(c)
print ("C. Output 3")
print (file1.read(9))
print ()
(d)
print ("D. Output 4")
print (file1.readline(9) )
(e)
print ("E. Output of Readlines function is")
print (file1.readlines() )
print ()

NOTE:  Consider the code fragments in succession, i.e., code (b) follows code (a), which means changes by code (a) remain intact. Similarly, code (e) follows (a) and (b), and so on.




Q5 = What is following code doing?
file = open("contacts.csv", "a")
name = input("Please enter name.")
phno = input("Please enter phone number.")
file.write (name + "," + phone + "\n")



Q6 = Write code to open file created in previous question and print it in following form:
Name : <name>    Phone :< phone number>




Q7 = Consider the file "contacts.csv" created in above Q. and figure out what the following code is trying to do?
name = input("Enter name :")
file = open("contacts.csv", "r")
for line in file:    
        if name in line:        
              print (line)


Q8 = Consider the file poemBTH.txt and predict the output of following code fragment. What exactly is following code fragment doing?
f = open ("poemBTH.txt", "r")
nl = 0
for line in f:
    nl += 1
print (nl)



Q9 = If you use the code of Q.8 with pl.txt created in solved problem 14, what would be its output?



Q10 = Write a method in python to read the content from a text file diary.txt line by line and display the same on screen.




Q11 = Write a method in python to write multiple line of text content into a text file mylife.txt line.



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

import pickle
ID = {1: "Ziva", 2: "53050", 3: "IT",4: "38",5: "Dunzo"}
fin = open("Emp.pkl","wb")
pickle.dump(EmpID, fin)
fin.close()
fout = open("Emp.pkl", 'rb')
ID = pickle.load(fout)
print( ID [5] )



 

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

import pickle
List1 = ['Roza', {'a': 23, 'b': True}, (1, 2, 3), [['dogs', 'cats'], None] ]
List2 = ['Rita', {'x': 45, 'y': False}, (9, 5, 3), [['insects', 'bees'], None] ]
with open('data.pkl', 'wb') as f:
    f.write(list1)

with open('data.pkl', 'wb') as f :
    f.write(list2)

with open('data.pkl', 'rb') as f :
    list1 = pickle.load(f)

print(list1)




Q14. What is the output of the following considering the file data.csv given on the right

File data.csv contains:

Identifier; First name; Last name
901242; Riya; Verma
207074; Laura; Grey
408129; Ali; Baig
934600; Manit; Kaur
507916; Jiva; Jain

import csv
with open('C:\data.csv','r+') as f:
    data = csv.reader(f)
    for row in data:
        if 'the' in row:
            print(row)





Q15. Identify the error in the following

import csv
f = open('attendees1.csv')
csv_f = csv.writer(f)



 

Q16. Identify the error in the following code.

import csv
f = open('attendees1.csv')
csv_f = csv.reader()
for row in csv_f:
    print(row)




Q17. Identify the error in the following code.

import pickle
data = ['one', 2, [3, 4, 5]]
with open('data.dat', 'wb':
          pickle. dump(data)


11 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post