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

Day 10 Live PDF

The document explains how to read and manipulate text files in Python. It covers opening and reading files, reading lines and characters, splitting files into words, and counting characters, words, lines, vowels, and more.

Uploaded by

Vinayak Gupta
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)
33 views11 pages

Day 10 Live PDF

The document explains how to read and manipulate text files in Python. It covers opening and reading files, reading lines and characters, splitting files into words, and counting characters, words, lines, vowels, and more.

Uploaded by

Vinayak Gupta
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

CLASS 12 COMPUTER SCIENCE PYTHON

PYTHON TEXT FILES | PART 02

IMPORTANT QUESTION
CHAPTER EXPLANATION
By : ANAND SIR, YOUTUBE CHANNEL: CODEITUP
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP
#read the content of a file called "student.txt" #using readline function
f=open("student.txt","r") f=open("student.txt","r")
data=f.read() data=f.readline()
print(data) print(data,end='')
f.close() data=f.readline()
print(data,end='')
f.close()

#read the content of a file called "student.txt" #using readline function


f=open("student.txt","r") f=open("student.txt","r")
data=f.read(10) data=f.readline(5)#it will read 5 character
print(data) print(data)
data=f.read(10) f.close()
print(data)
f.close()
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

#using readlines function print(open("student.txt","r"))


f=open("student.txt","r")
data=f.readlines()
print(data)
f.close() #Program: Find no of characters in a file 'student.txt'
f=open("student.txt")
data=f.read()
print("Total number of characters=",len(data))
#using readlines function f.close()
f=open("student.txt","r")
data=f.readlines()
for i in data: #Program 3: Find no of words in a file 'student.txt'
print(i,end='') f=open("student.txt")
f.close() data=f.read()
data1=data.split()
print(data1)
print("Total number of Words=",len(data1))
f.close()
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

#Program 3: Count total vowels and consonants #Program 3: Print last line
f=open("student.txt") f=open("student.txt")
data=f.read() data=f.readlines()
v=c=0 print(data[-1])
for i in data: f.close()
if i.isalpha():
if i in 'aeiouAEIOU':
v+=1
#count total sentences.
else:
f=open("student.txt")
c+=1
count=0
print("Total Vowels=",v,"Total COns=",c)
data=f.read()
f.close()
for i in data:
if i=='.':
count+=1
print("total Number of Sentences=",count)
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

#count total number of 'We' in a file "student.txt".


f=open("student.txt") #count total number of ‘He’ and ‘She’ in a file
count=0 "student.txt".
data=f.read() f=open("student.txt")
data1=data.split() count=0
for i in data1: count1=0
if i.upper()=='WE': data=f.read()
count+=1 data1=data.split()
print("Total Number of We=",count) for i in data1:
f.close() if i.upper()=='HE':
count+=1
if i.upper()=='SHE':
count1+=1
print("Total Number of He=",count,"Total Number of
She=",count1)
f.close()
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

#Program 3: Find no of words in a file 'student.txt' #Program 3: Count total vowels and consonants
f=open("student.txt") f=open("student.txt")
data=f.read() data=f.read()
data1=data.split() v=c=0
print(data1) for i in data:
print("Total number of Words=",len(data1)) if i.isalpha():
f.close() if i in 'aeiouAEIOU':
v+=1
else:
c+=1
#Program 2: Find no of lines in a file 'student.txt' print("Total Vowels=",v,"Total COns=",c)
f=open("student.txt") f.close()
data=f.readlines()
print("Total number of lines=",len(data))
f.close()
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

#Print only those lines that start with 'You'.


f=open("student.txt")
count=0 #Display those words having less that 4 characters
data=f.readlines() def count():
for i in data: f=open("student.txt")
x=i.split() data=f.readlines()
if x[0].upper()=='WE': for i in data:
print(i,end='') x=i.split()
f.close() for j in x:
if len(j)<4:
print(j)
f.close()
#__main__
count()
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP
TEXT FILES | PART 02
E X P L A N A T I O N B Y : A N A N D S I R , Y O UT UB E C H A N N E L : C O D E I T UP

You might also like