File Handling

[Set – 2]

1. Write a function in python to read the content from a text file "poem.txt" line by line and display the same on screen. Solution

2. Write a function in python to count the number of lines from a text file "story.txt" which is not starting with an alphabet "T". 

Example: If the file "story.txt" contains the following lines: A boy is playing there.
There is a playground.
An aeroplane is in the sky.
The sky is pink.
Alphabets and numbers are allowed in the password.

The function should display the output as 3 Solution

3. Write a function in Python to count and display the total number of words in a text file. Solution

4. Write a function in Python to read lines from a text file "notes.txt". Your function should find and display the occurrence of the word "the".

For example: If the content of the file is:
"India is the fastest-growing economy. India is looking for more investments around the globe. The whole world is looking at India as a great market. Most of the Indians can foresee the heights that India is capable of reaching."

The output should be 5. Solution

5. Write a function display_words() in python to read lines from a text file "story.txt", and display those words, which are less than 4 characters. Solution

6. Write a function in Python to count the words "this" and "these" present in a text file "article.txt". [Note that the words "this" and "these" are complete words] Solution

7. Write a function in Python to count words in a text file those are ending with alphabet "e". Solution

8. Write a function in Python to count uppercase character in a text file. Solution

9. A text file named "matter.txt" contains some text, which needs to be displayed such that every next character is separated by a symbol "#". Write a function definition for hash_display() in Python that would display the entire content of the file matter.txt in the desired format.

Example :
If the file matter.txt has the following content stored in it :
THE WORLD IS ROUND

The function hash_display() should display the following content :
T#H#E# #W#O#R#L#D# #I#S# #R#O#U#N#D# Solution

10. Aditi has used a text editing software to type some text. After saving the article as WORDS.TXT, she realised that she has wrongly typed alphabet J in place of alphabet I everywhere in the article.

Write a function definition for JTOI() in Python that would display the corrected version of entire content of the file WORDS.TXT with all the alphabets "J" to be displayed as an alphabet "I" on screen.

Note: Assuming that WORD.TXT does not contain any J alphabet otherwise.

Example:
If Aditi has stored the following content in the file WORDS.TXT:
WELL, THJS JS A WORD BY JTSELF. YOU COULD STRETCH THJS TO BE A SENTENCE
The function JTOI() should display the following content:
WELL, THIS IS A WORD BY ITSELF. YOU COULD STRETCH THIS TO BE A SENTENCE Solution

11. Write a function AMCount() in Python, which should read each character of a text file STORY.TXT, should count and display the occurance of alphabets A and M (including small cases a and m too).
For Example:
If the file content is as follows:
Updated information
As simplified by official websites.
The EUCount() function should display the output as:
A or a:4
M or m :2Solution

12. A binary file "Book.dat" has structure [BookNo, Book_Name, Author, Price].
i. Write a user defined function createFile() to input data for a record and add to Book.dat.
ii. Write a function countRec(Author) in Python which accepts the Author name as parameter and count and return number of books by the given Author are stored in the binary file "Book.dat" Solution

13. A binary file "STUDENT.DAT" has structure (admission_number, Name, Percentage). Write a function count_rec() in Python that would read contents of the file "STUDENT.DAT" and display the details of those students whose percentage is above 75. Also display number of students scoring above 75% Solution

14. Given a binary file employee.dat, created using dictionary object having keys: (empcode, name, and salary)

  1. Write a python function that add one more record at the end of file.
  2. Write a python function that display all employee records whose salary is more that 30000
Solution

15. Write a function to search and display details of student whose rollno is '1005' from the binary file student.dat having structure [rollno, name, class and fees]. Solution

16. A binary file school.dat has structure(rollno, name, class, fees)

Write a definition for function total_fees( ) that reads each object of file and calculate the total fees of students and display the same. Solution

17. A binary file players.dat, containing records of following list format: [code, name, country and total runs]

  1. Write a python function that display all records where player name starts from 'A'
  2. Write a python function that accept country as an argument and count and display the number of players of that country.
  3. Write a python function that add one record at the end of file.
Solution

18. Given a binary file game.dat, containing records of following list format: [game_name, participants]

Write a function in Python that would read contents from the file game.dat and creates a file named basket.dat copying only those records from game.dat where the game name is "Basket Ball" Solution