0% found this document useful (0 votes)
526 views4 pages

File Handling Questions 2

The document describes functions related to reading, writing, manipulating and analyzing text files in Python. Some key functions include: 1. Counting and displaying the number of words starting with certain letters, lines ending with vowels, or occurrences of specific words in a text file. 2. Copying the contents of one text file to another, potentially modifying the contents by removing or replacing characters like spaces. 3. Reading text files and displaying certain parts of the contents like specific words, lines matching criteria, or the last few characters/lines. 4. Performing analyses of text file contents like counting letters, characters, or words and displaying the results. Input and output involves getting file and string inputs from

Uploaded by

BHUSHAN GUPTA
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)
526 views4 pages

File Handling Questions 2

The document describes functions related to reading, writing, manipulating and analyzing text files in Python. Some key functions include: 1. Counting and displaying the number of words starting with certain letters, lines ending with vowels, or occurrences of specific words in a text file. 2. Copying the contents of one text file to another, potentially modifying the contents by removing or replacing characters like spaces. 3. Reading text files and displaying certain parts of the contents like specific words, lines matching criteria, or the last few characters/lines. 4. Performing analyses of text file contents like counting letters, characters, or words and displaying the results. Input and output involves getting file and string inputs from

Uploaded by

BHUSHAN GUPTA
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/ 4

1.

Consider the following code:


ch = "A"
f=open("data.txt",'a')
f.write(ch)
print(f.tell())
f.close()
What is the output if the file content before the execution of the program is the
string "ABC"? (Note that" " are not the part of the string)
2. Write a function to count and display the number of blanks present in a text file
named “PARA.TXT”.
3. Write a single statement to display the contents of a text file named "abc.txt"
4. Assuming that a text file named TEXT1.TXT already contains some text written
into it, write a function named vowelwords(), that reads the file TEXT1.TXT
and creates a new file named TEXT2.TXT, which shall contain only those words
from the file TEXT1.TXT which don’t start with an uppercase vowel (i.e., with
‘A’, ‘E’, ‘I’, ‘O’, ‘U’). For example, if the file TEXT1.TXT contains
Carry Umbrella and Overcoat When It rains
Then the text file TEXT2.TXT shall contain
Carry and When rains
5. Write a function in PYTHON to count the number of lines ending with a vowel
from a text file “STORY.TXT’.
6. Write a function in PYTHON to count and display the number of words starting
with alphabet ‘A’ or ‘a’ present in a text file “LINES.TXT”. Example:
If the file “LINES.TXT” contains the following lines,
A boy is playing there. There is a playground.
An aeroplane is in the sky.
Are you getting it?
The function should display the output as 5.
7. Write a function in PYTHON to display the last 3 characters of a text file
“STORY.TXT’.
8. Write a function copy() that will copy all the words starting with an uppercase
alphabet from an existing file “FAIPS.TXT” to a new file “DPS.TXT”.
9. Write a function show(n) that will display the nth character from the existing
file “MIRA.TXT”. If the total characters present are less than n, then it should
display “invalid n”.
10. Write a function in PYTHON that counts the number of “Me” or “My” words
present in a text file “DIARY.TXT”. If the “DIARY.TXT” contents are as follows:
My first book was Me and My
Family. It gave me chance to be
Known to the world.
The output of the function should be:
Count of Me/My in file: 4
11. Write a function in PYTHON to read the contents of a text file “Places.Txt” and
display all those lines on screen which are either starting with ‘P’ or with ‘S’.
12. Write a function CountHisHer() in PYTHON which reads the contents of a text
file “Gender.txt” which counts the words His and Her (not case sensitive)
present in the file.
For, example, if the file contains:
Pankaj has gone to his friend’s house. His frien’s name
is Ravya. Her house is 12KM from here.
The function should display the output:
Count of His: 2
Count of Her: 1
(CBSE 2012)
13. Write a function EUCount() in PYTHON, which should read each character of a
text file IMP.TXT, should count and display the occurrences of alphabets E and U
(including small cases e and u too).
Example:
If the file content is as follows:
Updated information
Is simplified by official websites.
The EUCount() function should display the output as:
E:4
U:1
14. Write function definition for SUCCESS( ) in PYTHON to read the content of a text
file STORY.TXT, count the presence of word SUCCESS and display the number of
occurrence of this word.
Note :
– The word SUCCESS should be an independent word
– Ignore type cases (i.e. lower/upper case)
Example :
If the content of the file STORY.TXT is as follows :
Success shows others that we can do it. It is possible to
achieve success with hard work. Lot of money does not
mean SUCCESS.

The function SUCCESS( ) should display the following :


15. Write3 function definition for TOWER() in PYTHON to read the content of a text
file WRITEUP.TXT, count the presence of word TOWER and display the number of
occurrences of this word. (CBSE-Outside Delhi 2015)
Note :
‐ The word TOWER should be an independent word
‐ Ignore type cases (i.e. lower/upper case)

Example:
Tower of hanoi is an interesting problem.
Mobile phone tower is away from here. Views
from EIFFEL TOWER are amazing.

If the content of the file WRITEUP.TXT is as follows:

The function TOWER () should display the following:

16. Write3 the function definition for WORD4CHAR() in PYTHON to read the content
of a text file FUN.TXT, and display all those words, which have four characters
in it.
Example:

When I was a small child, I used to play in the


garden with my grand mom. Those days were amazingly
funful and I remember all the moments of that time

If the content of the file Fun.TXT is as follows:

The function WORD4CHAR() should display the following:


17. Write
When function definition
used play with daysforwere
DISP3CHAR()
that timein PYTHON to read the content of a
text file KIDINME.TXT, and display all those words, which have three characters
in it.
Example:
If the content of the file KIDINME.TXT is as follows:

When I was a small child, I used to play in the


garden
with my grand mom. Those days were amazingly
funfilled and I remember all the moments of
that time

The function DISP3CHAR() should display the following:

was the mom and all the


18. Write a function in Python to copy the contents of a text file into another text
file. The names of the files should be input from the user.

19. Write a function in Python which accepts two text file names as parameters. The
function should copy the contents of first file into the second file in such a way
that all multiple consecutive spaces are replaced by a single space. For
example, if the first file contains:
Self conquest is the
greatest victory .
then after the function execution, second file should contain:
Self conquest is the
greatest victory .

20. Write a function in Python to input a multi-line string from the user and write
this string into a file named ‘Story.txt’. Assume that the file has to be created.

21. Write a function to display the last line of a text file. The name of the text file
is passed as an argument to the function.

22. Write a Python function to read and display a text file 'BOOKS.TXT'. At the end
display number of lowercase characters, uppercase characters, and digits
present in the text file.

23. Write a Python function to display the size of a text file after removing all the
white spaces (blanks, tabs, and EOL characters).

You might also like