Python File Handling Programs Important for Class 12

Share with others

Python File Handling Programs

1. Text – File Programs

2. Binary – File Programs

3. CSV – File Programs (Coming Soon)

Python File Handling Programs-Text File

Python File Handling Programs
Python File Handling Programs

Q1. Write a program in python to replace all word “the” by another word “them” in a file “poem.txt”.

Q2. Write a program in python to replace a character by another character in a file “story.txt”. (Accept both the characters from the user)



Python File Handling Programs

Q3. Write a program in python to replace all the ‘a’ by ‘@’ in a file “data.txt”.

Q4. Write a program in python to read file “data.txt” and display only those lines whose length is more than 40 characters.

Python File Handling Programs

Q5. Write a program in python to remove all duplicate lines from the file “story.txt”.


Q6. Write a program in python to display only unique words from the file “story.txt”.



Python File Handling Programs

Q7. Write a program in python to count the frequency of each vowels in a file “task.txt”.

Q8. Write a program in python to count those words whose length is more than 7 characters in a file “story.txt”.

Python File Handling Programs

Q9. Write a program in python to count those lines from the file “div.txt” which are starting from ‘T’ or ‘M’.

Python File Handling Programs
Python File Handling Programs

Q10. Write a program in python to count those lines from the file “div.txt” which are not starting from ‘M’.

Q11. Write a program in python to display those words from a file “image.txt” which are ending from alphabet ‘m’.

Q12. Write a program in python to read all lines of file “data.txt” using readline() only.

Python File Handling Programs

Q13. Write a program in Python to copy the entire content from file “data.txt” to “story.txt”

Q14. Write a program in Python to copy the alternate lines from file “data.txt” to “story.txt”

Q15. Write a program in Python to read the entire content from file “data.txt” and copy the contents to “story.txt” in upper case.

Python File Handling Programs

Q16. Write a program in Python to read the entire content from file “data.txt” and copy only those words to “story.txt” which start from vowels.

Q17. Write a program in Python to read the entire content from file “data.txt” and copy only those words in separate lines to “story.txt” which are starting from lower case alphabets .

Q18. Write a program in Python to read file “data.txt” and copy only those lines to “story.txt” which are starting from alphabets “A” or “T”.

Python File Handling Programs
Python File Handling Programs

Python File Handling Programs

Q19. Write a program in Python which display the longest word from file “star.txt”

Q20. Write a program in Python which display the longest line from file “star.txt”

Python File Handling Programs

Q21. Write a program in Python to read the file “star.txt” and display the entire content after removing leading and trailing spaces.

Q22. Write a program in python that read the content from file “sumit.txt” and display all numbers.

Q23. Write a program in Python that display the second and second last line from the file “life.txt”

Python File Handling Programs
Python File Handling Programs

Python File Handling Programs-Binary File

Q24. Consider a binary file “data.dat” which stores the record of “Hotel” in the form of list containing Room_no, Price, Room_type. Do the following task in a file

  1. Write a function addrec() to add a record in a file.
  2. Write a function disp() to display all the records from the file.
  3. Write a function specific_disp(room_no) which takes room number as argument and display its details.
  4. Write a function mod(room_no) which takes room number as argument and modify it’s details.
  5. Write a function del(room_no) which takes room number as argument and delete it’s record from file “data.dat”

Q25. Write a menu driven program which shows all operations on Binary File

  1. Add Record
  2. Display All Record
  3. Display Specific Record
  4. Modify Record
  5. Delete Record

Use “data.dat” file which stores the record of “Hotel” in the form of list containing Room_no, Price, Room_type.

Python File Handling Programs

Q26. Write a function disp75() in Python to display only those records of students from file “school.dat” who scored more than 75 percent marks. Structure stored in “school.dat” is in the form of list containing information like [rollno, name, class, percentage]

Q27. Write a function dispname() in Python which will display only names of all the students from file “school.dat”. Structure stored in “school.dat” is in the form of list containing information like [rollno, name, class, percentage]

Q28. Write a function disp12() in Python which will display records of class 12th students from file “school.dat”. Structure stored in “school.dat” is in the form of list containing information like [rollno, name, class, percentage]

Python File Handling Programs

Q29. Write a function search(name) in Python which will display record of a student from file “school.dat” whose name is passed as an argument. Structure stored in “school.dat” is in the form of list containing information like [rollno, name, class, percentage]

Q30. Write a function rem() in Python which will delete the records of students from file “school.dat” who scored less than 30 percent marks. Structure of “school.dat” is in the form of list containing information like [rollno, name, class, percentage].

Q31. Write a function modi_rec(rno) in Python which will modify the records of students from file “school.dat” whose Roll number is passed as an argument. Structure of “school.dat” is in the form of list containing information like [rollno, name, class, percentage].

Python File Handling Programs

Q32. Write a function disp_author(name) in Python to display detail of author whose name is passed as an argument from “book.dat”. Structure stored in file is in the form of [Bookid, Price, Author Name, Subject]


     

Q33. Write a function addmarks(name) in Python which will add the marks of all the subjects of a student whose name is passed as an argument from “marks.dat”. Structure of “marks.dat” is [Name, Chem_marks, Phy_marks, CS_marks, Eng_marks]


     

Q34. Write a function addval() in Python which will add the record of a mobile in “mobile.dat”. Structure of “mobile.dat” is [Mobile id, Mobile brand, Model No., Price]

Q35. Write a function disp_mob(model no.) in Python which will display the record of a mobile from “mobile.dat” whose model number (integer type) is passed as an argument. Structure of “mobile.dat” is [Mobile id, Mobile brand, Model No., Price]


Disclaimer : I tried to give you the correct coding of ” Python File Handling Programs ” , but if you feel that there is/are mistakes in the Answers of “ Python File Handling Programs “ given above, you can directly contact me at csiplearninghub@gmail.com. NCERT book and CBSE study material is used to create “Python File Handling Programsquestions and answers.


Python File Handling Programs


MCQ of Computer Science Chapter Wise

1. Functions in Python

2. Flow of Control (Loop and Conditional statement)

3. 140+ MCQ on Introduction to Python

4. 120 MCQ on String in Python

5. 100+ MCQ on List in Python

6. 50+ MCQ on Tuple in Python

7. 100+ MCQ on Flow of Control in Python

8. 60+ MCQ on Dictionary in Python


100 Practice Questions on Python Fundamentals

120+ MySQL Practice Questions

90+ Practice Questions on List

50+ Output based Practice Questions

100 Practice Questions on String

70 Practice Questions on Loops

70 Practice Questions on if-else

Python File Handling Programs

Python File Handling Programs

Python File Handling Programs

Python File Handling Programs


Share with others

Leave a Reply

%d bloggers like this: