0% found this document useful (0 votes)
6 views7 pages

file-handling-text-file-questions

The document contains Python code snippets for analyzing a text file named 'Poem.txt'. It includes functions to count occurrences of specific words ('you', 'and', 'AND'), words of certain lengths, and words starting with specific letters. Each section of code reads the file, processes the text, and prints the results for various queries.

Uploaded by

Shawrya Saxena
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)
6 views7 pages

file-handling-text-file-questions

The document contains Python code snippets for analyzing a text file named 'Poem.txt'. It includes functions to count occurrences of specific words ('you', 'and', 'AND'), words of certain lengths, and words starting with specific letters. Each section of code reads the file, processes the text, and prints the results for various queries.

Uploaded by

Shawrya Saxena
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/ 7

IMPORTANT QUESTIONS

1.total number of YOU or you present in text

f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in range(len(t)):
if t[i]=="you" or t[i]=="YOU":

c=c+1
print("total no of and or AND present:
", c)
f.close()

1.total number of YOU or you present in text


alternate

f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in t:
if i=="you" or i=="YOU":

c=c+1
print("total no of and or AND present:
", c)
f.close()

2.total number of words with length 3 present


in text

f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in range(len(t)):
if (len(t[i])==3):

c=c+1
print("total no of and or AND present:
", c)
f.close()
3.total number of words with length 3 present
in text alternate
f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in t:
if (len(i)==2):

c=c+1
print("total no of word of length: ", c)
f.close()

4.total number of and or AND present in text


f=open("C:/Users/Admin/Desktop/Poem.t
xt","r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in t:
if i=="AND" or i=="and":

c=c+1
print("total no of and or AND present:
", c)
f.close()

6. total number of and or AND present in text


alternate
f=open("C:/Users/Admin/Desktop/Poem.txt","r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in range(len(t)):
if t[i]=="AND" or t[i]=="and":

c=c+1
print("total no of and or AND present: ", c)
f.close()

7.total no of words present starting with T or


t

f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in t:
if i[0]=='t' or i[0]=='T':
c=c+1
print("Total no of words starting with t or T in
text file: ",c)
f.close()

8.total no of words present starting with T or


t alternate
f=open("C:/Users/Admin/Desktop/Poem.txt","r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in range(len(t)):
if t[i][0]=='t' or t[i][0]=='T':
c=c+1
print("Total no of words starting with t or T in
text file: ",c)
f.close()

9.Total no of words in a file

f=open("C:/Users/Admin/Desktop/Poem.txt","
r")
c=0
r=f.read()
print(r)
t=r.split()
print(t)
for i in t:
c=c+1
print("Total no of word in text file: ",c)
f.close()

10. Total no of words in a file alternate

f=open("C:/Users/Admin/Desktop/Poem.txt","r")
r=f.read()
print(r)
t=r.split()
print(t)
print(len(t))
f.close()

You might also like