Data File Handling || Preeti Arora || Class 12 || Unsolved Question || Computer science || Solution

 

 

Q1. What is the difference between "w" and "a" modes?



Q2. What is the significance of file-object?



Q3. How are open() functions different from close() functions other than their functionality of opening and closing files?



Q4. Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying the file path in two different formats.



Q5. In which of the following file modes will the existing data of the file not be lost?

(a) 'rb'
(b) ab
(c) w
(d) w + b
(e) 'a+b'
(f) wb
(g) wb+
(h) w+
(i) r+




Q6. What would be the data type of variable data in the following statements?

(a) data = f.read()
(b) data = f.read(10)
(c) data = f.readline()
(d) data = f.readlines()




Q7. When a file is opened for output, what happens when?

(i) The mentioned file does not exist
(ii) The mentioned file does exist




Q8. What role is played by file modes in file operations?
Describe the various file mode constants and their meanings.




Q9. Write a code snippet that will create an object called fileout for writing, associate it with the filename STRS. The code

should keep on writing strings to it as long as the user wants.




Q10. Explain how many file objects would you need to create to manage the following situations:

(a) To process three files sequentially
(b) To merge two sorted files into a third file.




Q11. What are the advantages of saving data in:

(i) Binary form
(ii) Text form




Q12. When do you think text files should be preferred over binary files?



Q13. Write a program that reads a text file and creates another file that is identical except that every sequence of consecutive

blank spaces is replaced by a single space.




Q14. A file sports.dat contains information in following format:
Event ~ Participant
Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those

records from sports.dat where the event name is "Atheletics ".




Q15. Write a program to count the words "to" and "the" present in a text file "Poem.txt".



Q16. Write a program to count the number of uppercase alphabets present in a text file "Pome.txt".




Q17. Write a program that copies one file to another. Have the program read the file names from user?



Q18. Write a program that appends the contents of one file to another. Have the program take the filenames from the user.



Q19. Write a program that reads characters from the keyboard one by one. All lower case characters get stored inside the file

LOWER, all upper case characters get stored inside the file UPPER and all other characters get stored inside file OTHERS.




Q20. Write a program to search the names and addresses of persons having age more than 30 in the data list of persons.



Q21. Write a function in Python to count and display the number of lines starting with alphabet 'A' present in a text file

"LINES.TXT". e.g., the file "LINES.TXT" contains the following lines:

A boy is playing there.
There is a playground.
An aeroplane is in the sky.
Alphabets & numbers are allowed in password.

The function should display the output as 3.




Q22. Write a function to insert a sentence in a text file, assuming that text file is very big and can't fit in computer's memory.



Q23. Write a program to read a file 'Story.txt' and create another file, storing an index of Story.txt, telling which line of the file

each word appears in. If word appears more than once, then index should show all the line numbers containing the word.




Q24. Write a program to accept a filename from the user and display all the lines from the file which contain Python comment

character '#'.




Q25. Reading a file line by line from the beginning What if you want to read a file backward? This happens when you need to

read log files. Write a program to read and display content of file from end to beginning.




Q26. Write a Python program to display the size of a file after removing EOL characters, leading and trailing white spaces and

blank lines.



Q27. Write a function Remove_Lowercase() that accepts two file names, and copies all lines that do not start with lowercase

letter from the first file into the second file..




Q28. Write a program to display all the records in a file along with line/record number.



Q29. Write a method in Python to write multiple line of text contents into a text file mylife.txt.



Q30. Write a method in Python to read the content from a text file DIARY.TXT line by line and display the same on the screen.



Q31. Write appropriate statements to do the following:

(a) To open a file named "RESULT.DAT" for output.
(b) To go to the end of the file at any time.



Q32. Write a program to add two more employees' details to the file "emp.txt" already stored in disk



Q33. How are the following codes different from one another?

(a)
fp = open("file.txt", "r")
fp.read()

(b) fp = open ("file.txt", 'r')




Q34. What is the output of the following code fragment? Explain.

fout = file ("output.txt", 'w')
fout.write("Hello world! \n")
fout.write("How are you?")
fout.close()
file("output.txt").read()



Q35. Write the output of the following code with justification if the contents of the file ABPathwala.txt are:

Welcome to Python Programming!

fl = file ("ABPathwala.txt", "r")
size = len (fl.read ())
print (size)
data = f1.read (5)
print (data)




Q36. Anant has been asked to display all the students who have scored less than 40 for Remedial Classes, Write a user-defined

function to display all those students who have scored less than 40 from the binary file "Student.dat".



Q37. Give the output of the following snippet:

import pickle
list1, list2 = [2, 3, 4, 5, 6, 7, 8, 9, 10], [ ]
for i in list1 :
    if (i % 2 == 0 and i % 4 == 0) :
        list2.append (i)
f = open ("bin.dat", "wb")
pickle.dump (list2, f)
f.close()
f = open ("bin.dat", "rb")
data = pickle. load (f)
f.close()
for i in data :
    print (i)




Q38. Following is the structure of each record in a data file named "PRODUCT.DAT".

("prod_code": value, "prod_desc": value, "stock": value)

The values for prod_code and prod_desc are strings and the value for stock is an integer.

Write a function in Python to update the file with a new value of stock. The stock and the product_code, whose stock is to be

updated, are to be inputted during the execution of the function.




Q39. Given a binary file "STUDENT.DAT", containing records of the following type:

[S_Admno, S_Name, Percentage]

Where these three values are:

S_Admno - Admission Number of student (string)
S_Name - Name of student (string)
Percentage - Marks percentage of student (float)

Write a function in Python that would read contents of the file "STUDENT.DAT" and display the details of those students

whose percentage is above 75.




Q40. Write statements to open a binary file C:\Myfiles\Text1.txt in read and write mode by specifying the file path in two different formats.



Q41. What are the advantages of saving data in:

(i) Binary form
(ii) Text form




Q42. When do you think text files should be preferred over binary files?



Q43. Write a statement in Python to perform the following operations:

(a) To open a text file "BOOK.TXT" in read mode.
(b) To open a text file "BOOK.TXT" in write mode.




Q44. 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")




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




Q46. 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)




Q47. Create a CSV file "Groceries" to store information of different items existing in a shop. The information is to be stored

w.r.t. each item code, name, price, qty. Write a program to accept the data from user and store it permanently in CSV file.

 

7 Comments

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

  1. Replies
    1. Please click on question to get answer of that question

      Delete
  2. question 44 is different from the text book question can u please check and tell

    ReplyDelete
  3. there are total 51 ques

    ReplyDelete

Post a Comment

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

Previous Post Next Post