0% found this document useful (0 votes)
309 views3 pages

File Handling

The document describes 15 functions related to reading from, writing to, and manipulating text files in Python. The functions include reading files line by line or word by word, counting words or lines matching certain criteria, finding and counting occurrences of words, modifying file contents by replacing characters, and adding or extracting records from pickled files containing dictionary or list data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
309 views3 pages

File Handling

The document describes 15 functions related to reading from, writing to, and manipulating text files in Python. The functions include reading files line by line or word by word, counting words or lines matching certain criteria, finding and counting occurrences of words, modifying file contents by replacing characters, and adding or extracting records from pickled files containing dictionary or list data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

File Handling

[Set – 1]

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.

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

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

4. Write a function in Python to read lines from a text file "notes.txt", to 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.

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.

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]

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

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


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#

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

11. Given a pickled 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

12. Write a function to search and display details of student whose rollno is ‘1005’ from
the pickled file student.dat. Assume that the file student.dat is created with the help of
dictionary object having information rollno, name, class and fees.

13. Assume that the pickled file school.dat is created with the help of dictionary objects
which keys are given below:

(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.

14. Given a pickled 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.

15. Given a pickled 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"

You might also like