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

Python File Handling Programs

The document contains a series of Python programs designed to perform various file operations on a text file named 'story.txt'. These operations include counting vowels, specific letters, uppercase letters, occurrences of words, and modifying content by replacing words. Additionally, it includes functions for writing data to files and reading lines, along with practice questions for further learning.

Uploaded by

Nalin and Anjali
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 views8 pages

Python File Handling Programs

The document contains a series of Python programs designed to perform various file operations on a text file named 'story.txt'. These operations include counting vowels, specific letters, uppercase letters, occurrences of words, and modifying content by replacing words. Additionally, it includes functions for writing data to files and reading lines, along with practice questions for further learning.

Uploaded by

Nalin and Anjali
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

PART 1 - TEXT FILE

Example-

fout=open('[Link]','w')
[Link]("first line")
x=input("enter any key before flush")
[Link]()
x=input("enter any key after flush")
[Link]("second line")
[Link]()

Programs-
1 #write a program to count number of vowels in the file [Link]
fin=open("[Link]","r")
data=[Link]()
print(data)
c=0
for i in data:
if i in "aeiouAEIOU":
c=c+1
print("No. of vowels in file are :",c)
[Link]()

2 #write a program to count number of a in the file [Link]


fin=open("[Link]","r")
data=[Link]()
c=0
for i in data:
if i=="a":
c=c+1
print("No. of a in file are :",c)
[Link]()

3. # program to count number of upper letters in the file [Link]

#step 1 opening file


fin=open("[Link]","r")

#step 2 fetching data and process


data=[Link]()
print(data)
c=0
for i in data:
if [Link]():
c=c+1
print("total no. of a are:: ",c)

#step 3
[Link]()

4. # Program to count number of hello and there in the file [Link]


fin=open("[Link]","r")
data=[Link]()
print(data)
data1=[Link]()
print(data1)
c=0
for i in data1:
if [Link]()=="hello" or [Link]()=="there":
c=c+1
print("number of hello are::",c)
[Link]()

5. # Function to count words with length 5 characters


def count_5_word():
fin=open("[Link]","r")

data=[Link]()
words=[Link]()
print(words)
c=0
for i in words:
if len(i)==5:
c+=1
print("Total number of Words of Five characters are::",c)
[Link]()
count_5_word()

6. #write a program to count the lines starting with T in a file [Link]


fin=open("[Link]","r")
data=[Link]()
print(data)
c=0
for i in data:
if i[0]=='T':
c=c+1
print("Lines starting with T are::",c)
[Link]()

7. #write a program to count the lines with readline()

fin=open('[Link]' , "r")
c=0
line=" "
while line:
line=[Link]()
c=c+1
print(line)
[Link]()

8. #replace 'was' with 'is' and copying in other file


fin=open('[Link]','r')
fout=open('[Link]','w')
data=[Link]()
for i in data:
if 'was' in i:
[Link]([Link]('was','is'))
[Link]()
[Link]()

9 # function to write string in the text file using write()


fout=open('[Link]','w')

for i in range(0,4):
name=input('enter name')
[Link](name+'\n')

[Link]()

10 # writing string to text file using write()


fin=open('[Link]','w')
for i in range(0,3):
name=input('enter name')
marks=int(input('enter marks'))
rno=int(input('enter rno'))
s=str(rno)+" "+name+" "+str(marks)+"\n"
[Link](s)
[Link]()
11 # function to write name in the text file using writelines()
fout=open('[Link]','w')
l=[]
for i in range(0,4):
name=input('enter name')
[Link](name+'\n')
[Link](l)
[Link]()

12 # opening file using with statement


with open('[Link]','w') as fout:
for i in range(0,2):
name=input('enter name')
[Link](name+'\n')

SOME PRACTICE QUESTIONS-

You might also like