KEMBAR78
File Handling-Text Files | PDF | Text File | Function (Mathematics)
0% found this document useful (0 votes)
43 views4 pages

File Handling-Text Files

Uploaded by

Future Mrskim
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)
43 views4 pages

File Handling-Text Files

Uploaded by

Future Mrskim
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/ 4

FILE HANDLING-TEXT FILE

1. Write a program that writes 10 random numbers to a file


'numbers.txt'. Each random number should be in the range of 1
through 100.

2. Write a program that reads and display all of the numbers stored in
the file numbers.txt (created in question 1) and calculates their total.
3. Write a function, digit_count() in Python that counts and displays the
number of digits in the text file named 'sample.txt'. For example, if the
content of 'sample.txt' is as follows :

The team achieved a milestone in 2023. They completed a


multi-million-dollar project ahead of schedule. Stakeholders
were impressed with a 98% success rate.

The function should display the output as 6

4. Write a function lines_count() that reads lines from a text file named
'zen.txt' and displays the lines that begin with any vowel. Assume the
file contains the following text and already exists on the computer's
disk:
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.

The lines_count() function should display the output as:


Explicit is better than implicit.

5. Assume that the file 'notes.txt' containing some text and exists on the
computer’s disk. Write a program that display only those words from
'notes.txt' file whose length is more than seven. Keep in mind that any
punctuation marks at the beginning or end of a word should also be
considered as part of the word's length.
6. Write a function last_digit_words() in Python to count the words
ending with a digit in a text file "notes.txt". For example, if the file
content is as follows :

The Computer6 hums softly as I sit with a Book3 in hand,


diving into a world of imagination. Outside, my friends gather
at House9 and I quickly grab my Pen2 to jot down the address.

The expected output should be:


Number of words ending with a digit are 4

7. Assume that a file 'names.txt' containing a series of names (as strings)


exists on the computer’s disk. Write a function, first_five() that
displays only the first five lines of the file’s contents. If the file
contains less than five lines, it should display the file’s entire contents.
8. Write a Python program that reads a text file and prints its contents in
reverse order (from the last line to the first line).

9. Write the definition of a Python function named long_lines( ) which


reads the contents of a text file named 'lines.txt' and displays those
lines from the file which have at least 8 words in it. For example, if the
content of 'lines.txt' is as follows :

Flat is better than nested.


Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.

The output should be:


Special cases aren't special enough to break the rules.

10. Assume that a file named 'feedback.txt' contains student feedback in


the following format:
Positive: Saksham improved grades, more confident now.
Negative: Arav needs better time management for coursework.
Negative: Samar should work on communication in group
activities.
Negative: Soham could benefit from asking more questions in
class.
Positive: Sakshi excels academically, a great team player.

Write a Python function named feedback_analysis() to calculate and


display the following information:
Total feedbacks stored in the file.
Count of positive feedbacks.
Count of negative feedbacks.

11. Write a function in python to read the content from a text file
"poem.txt" line by line and display the same on screen.
12. 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

13. Write a function in Python to count and display the total number of

words in a text file.


14. 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.
15. 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 :2

You might also like