[go: up one dir, main page]

0% found this document useful (0 votes)
8 views11 pages

Text Files Programs III

Uploaded by

roughuse4md
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)
8 views11 pages

Text Files Programs III

Uploaded by

roughuse4md
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/ 11

TEXT FILES PROGRAMS III

1. Write a Python Program to read from the text file “Story.txt” and display
those words, which are ending with 'e'.
TEXT FILE
Difficult1 because2 we think that happiness is found Only in the places3 where
wealth4 and fame abound.And so we 6 go on searching in palaces8 of pleasure
Seeking recognition 5 and monetary treasure, Unaware that happiness is6 just a
state 2 of mind Within the reach of everyone who takes time to be kind. For in
making others happy 9 we will be happy, too. For the happiness you give away
returns to shine on you.
PROGRAM
def read():
f=open("Story.txt","r")
data=f.read()
words=data.split()
for i in words:
if i[-1]=='e':
print(i,end='\t')
read()
OUTPUT
we the where fame we pleasure Unaware state the everyone
time be we be the give shine
2. Write a Python Program to read fr text file “Story.txt" and calculate and
display the sum of all the even digits present in a text file.
PROGRAM
def read():
f=open("Story.txt","r")
sum=0
data=f.read()
for i in data:
if i.isdigit():
if int(i)%2==0:
print(i)
sum=sum+int(i)
print("Sum of even digits:",sum)
read()
OUTPUT
2
4
6
8
6
2
Sum of even digits: 28
3. Write a Python Program to read data from a text file "PythonSpace.txt" and
remove the multiple spaces with a single space.Write the data in a file.
TEXT FILE
Python is an interpreted, Created by Guido van Rossum and
philosophy emphasizes high-level, general-purpose programming language. first
released in 1991, Python's design code readability with its notable use of
significant whitespace. Its language constructs and object-oriented approach
aim to help programmers write clear, logical code for small and large-scale
projects. Python is dynamically typed and garbage collected. It supports
multiple programming paradigms, including structured (particularly,
procedural), object-oriented, and functional programming. Python
is often described as a "batteries included" language due to its comprehensive
standard library. Python was conceived in the a successor
to the ABC language. Python 2.0, released in 2000, like list late 1980s as
introduced features comprehensions and a garbage collection system with
reference counting. Python 3.0, released in 2008, was a major
revision of the language that is not completely backward-
compatible, unmodified on Python 3. and much Python 2 code does not run.
PROGRAM
def RemoveSpace():
f=open("PythonSpace.txt","r")
f1=open("PythonRemoveSpace.txt", "w")
data=f.read()
word=data.split()
s=' '.join(word)
f1.write(s)
print("Data Written Successfully")
RemoveSpace()
4. Write a Python Program to read data from a text file and print only the
numbers or digits from the file.
TEXT FILE
Some of the most recent final releases of versions:
Python 2.4.6 19 Dec 2008
Python 2.5.6 26 May 2011
Python 2.6.9 - 29 Oct 2013
Python 2.7.10 23 May 2015
Python 2.7.13 17 Dec 2016
Python 3.5.3 17 Jan 2017
Python 3.6.10 18 Dec 2019
python 3.8.2 24 feb 2020
PROGRAM
def CountDigits():
f=open("Versions.txt","r")
data=f.read()
for i in data:
if i.isdigit():
print(i,end=' ')
f.close()
CountDigits()
OUTPUT
2461920082562620112692920132710232015271317
20163531720173610182019382242020
5. Write a method in Python to read data from a text file Poem.txt and display
those words, which are less than 4 characters.
TEXT FILE
We wait for what we long for.
We long for what we need.
Impatience and anxiety Give root to errant deed,
Which grows to yield but heartache,
The fruit that poisons trust,
And so to shadow we recede And hope descends to dust.
But somehow from the ashes The will to try again
PROGRAM
def Display4Chars():
f=open("Poem.txt","r")
data=f.read()
word=data.split()
for i in word:
if len(i)<4:
print(i,end='\t')
Display4Chars()
OUTPUT
We for we We for we and to to but
The And so to we And to But the The to try
6. Write a Python Program to count the number of upper -case alphabets in a
text file "Poem.txt".
PROGRAM
def CountUpper():
f=open("Poem.txt","r")
count=0
data=f.read()
for i in data:
if i.isupper():
count+=1
print("Total Upper Case Alphabets:",count)
f.close()
CountUpper()
OUTPUT
Total Upper Case Alphabets: 10
7. Write a Python count the words "to" and "the" present in a text file
"Poem.txt"
PROGRAM
def CountWordsToThe():
f=open("Poem.txt","r")
count=0
data=f.read()
word=data.split()
for i in word:
if i=='to' or i=='the':
count+=1
print("Total words 'the' and 'to' :",count)
f.close()
CountWordsToThe()
OUTPUT
Total words 'the' and 'to' : 6
8. Write a Python Program to replace all the CAPITAL letters with their
respective small letters in the same file.
TEXT FILE
Hello And Welcome To Python Programming Tutorial...
PROGRAM
def CapitalReplace():
f=open("lower.txt","r+")
data=f.read()
f.seek(0)
for i in data:
if i.isupper():
f.write(i.lower())
else:
f.write(i)
f.close()
CapitalReplace()
OUTPUT
hello and welcome to python programming tutorial...
9. Write a Python Program to read content from a file "Article.txt" and display
the sum of all even digits present in a file.
TEXT FILE
H3llo and4 Welcome 2 my Tuotrials of Python6 Progr7amming.
PROGRAM
def SumEven():
f=open("Article.txt","r")
sum=0
data=f.read()
for i in data:
if i.isdigit():
if int(i)%2==0:
sum=sum+int(i)
print("Sum of even digits=",sum)
f.close()
SumEven ()
OUTPUT
Sum of even digits= 12
10. Write a Python Program to read data from a file. All lower case letters stored
inside the file LOWER, all upper case characters get stored inside the file
UPPER and all other characters stored inside the file OTHERS.
TEXT FILE
Some of the most recent final releases of versions:
Python 2.4.6 19 Dec 2008
Python 2.5.6 26 May 2011
Python 2.6.9 - 29 Oct 2013
Python 2.7.10 23 May 2015
Python 2.7.13 17 Dec 2016
Python 3.5.3 17 Jan 2017
Python 3.6.10 18 Dec 2019
python 3.8.2 24 feb 2020
PROGRAM
def TransferULO():
f=open("Versions.txt","r")
fu=open("UPPER.txt","w")
fl=open("LOWER.txt","w")
fo=open("OTHERS.txt","w")
data=f.read()
for i in data:
if i.isupper():
fu.write(i)
elif i.islower():
fl.write(i)
else:
fo.write(i)
f.close()
fu.close()
fl.close()
fo.close()
TransferULO()
11. Write a Python Program to read a random line from a file.
TEXT FILE
Some text of File 1 Line 1
Some text of File 1 Line 2
Some text of File 1 Line 3
Some text of File 1 Line 4
Some text of File 1 Line 5
Some text of File 1 Line 6
PROGRAM
import random
def ReadRandomLine():
f=open("File1.txt","r")
lines=f.readlines()
length=len (lines)
r1=random.randint(0,length-1)
print(lines[r1])
f.close()
ReadRandomLine()
OUTPUT
Some text of File 1 Line 6
12. Write a Python Program to display all the records in a file along with
line/record number.
TEXT FILE
With death being the only certainty of life,
life is just a bit too short for regrets.
Let's live while we're alive.
Let's love while we're alive.
Let's live and not just survive.
PROGRAM
def LineNumber():
f=open("Life.txt","r")
count=1
while True:
line=f.readline()
if line=='':
break
else:
print(count,":",line,end=' ')
count+=1
f.close()
LineNumber()
OUTPUT
1 : With death being the only certainty of life,
2 : life is just a bit too short for regrets.
3 : Let's live while we're alive.
4 : Let's love while we're alive.
5 : Let's live and not just survive.

You might also like