Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.

#Menu driven program to create and read contents of file


def add():
f=open("xiib.txt","w")
f.write("We all are Good Students\n")
f.write("We all love our country")
f.close()
def showline():
f=open("xiib.txt","r")
for line in f:
print(line)
f.close()
def showword():
f=open("xiib.txt","r")
for line in f:
for w in line.split():
print(w)

f.close()
def showchar():
f=open("xiib.txt","r")
for line in f:
for w in line:
print(w)

f.close()

while True:
print("1. Create File")
print("2. Read contents of File Line by Line")
print("3. Read contents of File Word by Word")
print("4. Read contents of File Char by char")
print("5. Exit")
ch=int(input("Enter Your Option (1-5) :"))
if ch==1:
add()
elif ch==2:
showline()
elif ch==3:
showword()
elif ch==4:
showchar()
elif ch==5:
break
else:
print("Invalid choice")

2. 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)
3. Write a function to count and display the number of blanks present in a text file
named “PARA.TXT”.
def count():
F=open(“para.txt”,”r”)
C=0
For line in f:
For char in line:
If char==” “:
C=c+1
Print( c )
F.close()

OR
def count():
F=open(“para.txt”,”r”)
A=f.read()
C=A.count(“ “)
print( c )
F.close()

4. Write a single statement to display the contents of a text file named "abc.txt"

5. 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
def vowelwords():
F=open(“text1.txt”,”r”)
F1=open(“text2.txt”,”w”)
Lines= F.readlines()
For word in Lines.split():
If word[0] not in “AEIOU”:
F1.write(word+” “)
F.close()
F1.close()

6. Write a function in PYTHON to count the number of lines ending with a vowel
from a text file “STORY.TXT‟.
7. 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.
8. Write a function in PYTHON to display the last 3 characters of a text file
“STORY.TXT‟.

A=f.readlines()
print(A[-1][-3:])

Or
A= f.read()
print(A[-3:])

9. 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 “OXFORD.TXT”.
10. 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”.
11. 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
12. 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‟.
13. 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
14. 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
15. 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 :

Success shows others that we can do it. It is possible to


achieve success with hard work. Lot of money does not
mean SUCCESS.
If the content of the file STORY.TXT is as follows :
3

The function SUCCESS( ) should display the following :


16. Write 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:

17. Write 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:


When used play with days were that time

18. Write function definition for DISP3CHAR() in 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


19. 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.
20. 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 .
21. 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.

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

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

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