0% found this document useful (0 votes)
102 views5 pages

File Handling - MD

The document contains a series of programming tasks related to file handling in Python. It includes functions for reading, writing, and manipulating various types of files such as text, CSV, and binary files. Each task specifies the required functionality and expected outcomes for handling data in these files.

Uploaded by

Poonam Varshney
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)
102 views5 pages

File Handling - MD

The document contains a series of programming tasks related to file handling in Python. It includes functions for reading, writing, and manipulating various types of files such as text, CSV, and binary files. Each task specifies the required functionality and expected outcomes for handling data in these files.

Uploaded by

Poonam Varshney
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/ 5

1. Given a binary file "Inventory.

dat" storing details of stock# File Handling Practice Questions

2. Write a function in Python to read a text file, Alpha.txt and displays those lines which begin
with the word 'You'.
3. Write a function, vowelCount () in Python that counts and displays the number of vowels in
the text file named Poem.txt.
4. Write a function countvowels () to open a text file "Rhymes.txt" and display the number of
vowels in it.
5. Write a function to count the number of lines starting and ending with "a" in a text file
"Notes.txt".
6. Write a method in Python to read lines from a text file DIARY. TXT, and display those lines,
which are starting with the letter 'P'.
7. Write a function countmy ( ) in Python to read the text file "DATA.TXT" and count the number
of times "my" occurs in the file.
For example if the file "DATA.TXT" contains:
"This is my website. I have displayed my preferences in the CHOICE section."
The countmy( ) function should display the output as: "my occurs 2 times".
8. Write a function in Python to count the number of blank spaces in a text file named
"STORY.txt".
9. Write a method COUNTLINES () in Python which are not starting with any vowel. to read lines
from text file 'TESTFILE.TXT' and display the lines
Example:
If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone's safety.
A marked difference will come in our country.
The COUNTLINES () function should display the output as:
The number of lines not starting with any vowel - 1
10. Write a function ETCount() in Python, which should read each character of a text file
"TESTFILE.TXT" and then count and display the count of occurrence of alphabets E and T
individually (including small cases e and t too).
Example:
If the file content is as follows:
Today is a pleasant day.
It might rain today.
It is mentioned on weather sites
The ETCount() function should display the output as:
E or e: 6
T or t: 9
11. What happens when we use file open () function in Python ?
12. Create file phonebook.txt that stores the details in following format:
Name Phone
Jivin 86666000
Kriti 1010101
Obtain the details from the user.
13. What is closed attribute of a file object?
14. A file phonebook.txt stores the details in the following format:
Name Phone
Jivin 86666000
Kriti 101001
Write a program to edit the phone numbers of "Arvind" in file. If there is no record for "Arvind"
report error.
15. Give any one point of difference between a binary file and a csv file.
16. Write a Program in Python that defines and calls the following user defined functions:
add_data() - To accept and add more data of products to a CSV file 'fundata.csv'. Each
record consists of a list with field elements as fid, fname and fprice to store furniture id, furniture
name and furniture price respectively.
search () - To display the records of the furniture whose price is more than 10000.
17. What is X mode in text file in python?
18. Write a function AMCCount() in Python, which should read each character of a text file
STORY.TXT, should count and display the occurrence of alphabets A and M (including small
cases a and m too).
Example:
If the file content is as follows:
Updated information
As simplified by official websites
The AMCount() function should display the output as:
A or a:4
M or m:2
19. Suhani is a student of class 12 and has been assigned by her faculty to write functions
Insert() and Detail() for working with details of students:
(i) Insert () To accept of a student to a CSV file "Detail.csv". Each detail consists of a list with
elements as Roll No, Name and Subject to store roll no, student name and prefer subject
respectively.
(ii) Detail () - To count the number of records present in the csv file named detail.csv.
20. Write a function to read the content of a text file "DELHI.txt" and display all those lines on
screen, which are either starting with 'D' or 'M'.
21. Write a function countarg (wrd) to count the number of times the word in the function
argument appears in a file "Tfile.txt" and display the count.
22. A csv file "students.csv" contains students name, score. Write a function score () - to read
students name, score from the file and print.
23. Create file marks. dat that stores the details in following format:
Name Marks
Rahul 750
Kiyaan 665
Obtain the details from the user.
24. Amritya Seth is a programmer, who is writing the code with following functions:
(a) AddStudents () to create a binary file called STUDENT. DAT containing student
information-roll number, name and marks (out of 100) of each student.
(b) GetStudents () to display the name and percentage of those students who have a
percentage greater than 75. In case there is no student having percentage > 75 the function
displays an appropriate message.The function should also display the average percent.
25. Define a function reverse() that overwrites the contents of the file "myfile.txt" with its reverse
contents. That is the file will now contain the lines of the original file in reverse order.
26. Write a program that prompts for a phone number of 10 digits and two dashes, with dashes
after the area code and the next three numbers.
e.g. 017-555-1212 is a legal input.
Display if the phone number entered is invalid format or not and display if the phone number
is valid or not.
27. How is r+ file mode different from rb+ mode?
28. Given a binary file "Hospital.dat" storing details of patients as per following structure : \[patId,
patname gender, wardno, doctor\]
Write a function transfer (wrd) to copy those records from the file to another file newfile.dat
whose ward number is provided as parameter.
29. Which method is used to add element to a list but at specified index number?
as per following structure: \[ItemId, Iname Qty Reorder, Category\]
Write a function Modify () to input an ItemId and update the item details in the file.
30. Mrs. Maria is a Programmer in a company. She wants to write a code to transfer data of
stock from a csv file "Stock.csv" that stores records of inventory of her company.
The file stores records of following structure:
\[StockID, Stockname, Brand Quality Price]
Help her to write a function transfer (Sid) that receives a stockid as argument and copies the
records of the stock to another file "Newstock.csv". The code should also display the contents of
the new file.
31. Write any one difference between writerow() and writerows() function for csv files.
32. Given a binary file "Tour.dat" storing details of tourist spots as per following structure: \[TId,
Tname No_of_tourists, price, Location]
Write a function addMore () to add details of more tours into the file keeping the existing
records.
33. What Is The Special Character That Terminates Each Line Of A Text File Called?
34. Given a binary file "Games.dat" storing details of games as per following structure:
\[GameID, Gname, Type Prizemoney]
Write a function Update (gid) that updates the record of the gameid in the binary file.
35. Mr. Surajit is a Librarian in a leading school. He wants to write a code "Books.csv" that
stores records of books in his library. to search data of books from a csv file
The file stores records of following structure:
\[BookID, Bname, Publisher Author Price]
Help him to write a function getBook (Pub) that receives a publisher name as argument and
displays details of the books of the publisher.
36. Define a function reverse() that overwrites the contents of the file "myfile.txt" with its reverse
contexts. That is the file will now contain the lines of the original file in reverse order.
37. Consider the file "Poem.txt"
Autumn Song
Like a joy on the heart of a sorrow,
The sunset hangs on a clouds,
A golden storm of glittering sheaves, Of fair and frail and flattering leaves,
The build wind blows in a cloud.
Define a function reverse( ) that prints the lines of the file in reverse order.
38. Raj, a programmer in a telephone company is writing a program that accepts a customer
type and displays records of telephone customers stored in a csv file "phone.csv". The file
contains records of phone customers of the following structure:
Cust id. Cname. Type. Bill
C123, "Raj", "Res", 6200
D324, "Mohan", "Offi", 7800
The program should input the type: "Residential" or "Official" and display all the records of the
specific type
Help him in writing the code.
39. Differentiate between file modes r+ and w+ with respect to Python.

40. Given a pickled log. dat file, containing list of strings. Write a python function that reads the
file and looks for a line of the form
Xerror: 0.2395
whenever such line is encountered, extract the floating point value and compute the total of
these error values. When you reach end of file print total number of such error lines and
average of error value.
41. What is the use of os.path module?
42. Write a program to use pickle module for reading and writing binary files.
43. Ravi is a programmer and has been assigned to write functions Add_data( ) and Search
data( ) for working with records of products.
(i) Add_data ( ) - To accept and add more data of products to a CSV file 'product.csv'. Each
record consists of a list with field elements as ProdId, Prodname and Qty to store Product ID,
Product name and qty of the products respectively.
(ii) Search_data (p) - To display the records present in the CSV file, whose productid matches
with the argument of the function, and display.
Help Ravi in writing the code.
44. Write a function showords () to open a text file "words.txt" and display the words which have
more than 5 letters.
45. Write a function to count the number of lines in a text file "players.txt".
46. Suman is a final semester student and has been assigned a project to write functions
Newrecord( ) and Displayrecords ( ) for working with records of students.
(i) Newrecord( ) - To accept and add data of a student to a CSV file 'student.csv'. Each record
consists of a list with field elements as StudId, Studname and Stream to store Student id,
Student name and stream of the student respectively.
(ii) Displayrecords( ) To display the records present in the CSV file named 'student.csv'. Help
suman in writing the code.

You might also like