You are on page 1of 3

FILE HANDLING IN PYTHON

ASSIGNMENT

Q1. What is the difference between text and binary files?

Q2. Compare and contrast read(), readline() and readlines().

Q3. How is write() different from writelines()?

Q4. Explain pickling and unpickling.

Q5. Explain the difference between write and append mode of opening a file.

TEXT FILES

Q6. Write a Python script to read a text file “Quotes.txt” and display only those lines that start
with “E”.

Q7. Write a Python script to read a text file “XYZ.txt” and count the number of lines which are
comments (i.e. which start from “#”).

Q8. Write a program to read a text file and display the words not starting with uppercase vowel.

Q9. WAP to read a text file and count number of lines staring by character inputted by user.

Q10. WAP to read a text file and to count number of times word "the" appears in file.

Q11. WAP to read a text file and count the number of lines ending with vowels.

Q12. Write a function to count the number of uppercase and lowercase alphabets and digits
present in a text file “STORY.TXT”.

Q13. Write a function to count the number of characters, words and lines present in a text file
“STORY.TXT”.

Q14. Write a program that reads a text file and displays the list of frequencies of words in it.

Q15. Write a program that reads a text file and displays all words which are less than 4
characters long.

Q16. Write a program to read a text file and create another text file toggling the case of all
alphabets present in that file.

Q17. Write a program to read a text file and create another text file after deleting all occurrences
of the words ending with “ing”.

Q18. Write a program to read a text file and create another text file replacing every occurrence
of consecutive blank spaces by a single space.

Q19. Write a program that reads a text file and count the number of words starting with “sh”.
Q20. Write a program that reads a text file and create a new file after adding “ing” to all words
ending with “t”, ”p” and “d”.

BINARY FILES

Q21. Create a dictionary having names of 5 states and their area. Store it in a binary file.

Q22. Write a Python script to input ItemNo, ItemName and Price of 10 items from the user and
store them in a binary file “ITEM.DAT”.

Q23. Write a Python script to input EmployeeNo, EmployeeName, HRA, DA, and BasicSalary
from the user. Calculate NetSalary as sum of BasicSalary, HRA and DA. Store this data for at
least 5 emplyees in a binary file EMPLOYEE.DAT.

Q24. Write a Python script to read a binary file having information DrinkCode, DrinkName,
DrinkPrice and DrinkSize. Display those records where DrinkPrice >250.

Q25. Write a Python script to read a binary file having information PlayerCode, PlayerName,
TotalRuns and PlayerCountry and perform the following operations:

• Input country from the user and count the number of players of that country
• Display all records where PlayerName starts from A
• Display records of players whose TotalRuns are more that 10000
• Display the name and country of player with maximum TotalRuns.

Q26. Write a Python script to create a binary file DICTIONARY.DAT and store at least 15
words and there meaning. Perform the following operations on this file:

• Search meaning of a word input by the user


• Add new words and there meaning
• Delete word and meaning entered by the user
• Modify the meaning of the word entered by the user

Q27. Write a Python script to create a binary file “Flight.Dat” containing information:
FlightNum, NoOfSeats, StartingPoint and Destination.

• Display all flights with Destination=”Delhi”


• Count all flights with NoOfSeats more than 150.
• Display the details of flight with given FlightNo.
• Change the Destination to “Mumbai” for flightNum=”A102”.

Q28. Write an interactive menu driven program to perform the following operations on a binary
file EMP.DAT having following fields: EMPID, SALARY, BENEFITS, DESIGNATION

• create a binary file


• read and display binary file
• append record
• modify record
Q29. Write an interactive menu driven program to perform the following operations on a binary
file CUSTOMER.DAT storing following information: CustCode, CustName, CustAddress,
CustArea, CustPhone

• Add new records in the file.


• Search and display all records of the area given by the user.
• Update the data file, where there is change in telephone no or address.
• Delete a record whose CustCode is given by the user

CSV FILES

Q30. Write a Python script to create a file CAMERA.CSV having fields ModelNo, MegaPixel,
Zoom and Details using comma as a delimiter.

Q31. Write a Python script to create a file CAMERA.CSV having fields ModelNo, MegaPixel,
Zoom and Details using tab as a delimiter.

Q32. Write a Python script to read a VOTER.CSV file containing Voter_Id, VoterName,
VoterAge having comma as a delimiter. Display those records where voter age is more than 65.

Q33. Write a Python script to read a STUDENT.CSV file containing RollNo, English, Hindi,
Science, SST, Maths marks. Display RollNo, total and percentage of all students. Calculate total
and percentage.

Q34. Write a Python script to read a PHONE.CSV file containing Name, Address, AreaCode
and PhoneNo. Display all records in ascending order of name.

[ FOR ANSWERS REFER LINK]

https://www.python4me.com/afilehandling

https://www.python4me.com/solbinary

https://www.python4me.com/solcsv

You might also like