0% found this document useful (0 votes)
21 views

Python File Handling Text Files 21st March 2025

The document contains notes by Anand Sir for CBSE Class 12 Computer Science focusing on Python file handling and text files. It includes various code examples for creating, writing, and reading files, as well as counting characters, words, lines, and vowels. The notes are part of the CODEITUP YouTube channel's educational resources.

Uploaded by

kashvi5907
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)
21 views

Python File Handling Text Files 21st March 2025

The document contains notes by Anand Sir for CBSE Class 12 Computer Science focusing on Python file handling and text files. It includes various code examples for creating, writing, and reading files, as well as counting characters, words, lines, and vowels. The notes are part of the CODEITUP YouTube channel's educational resources.

Uploaded by

kashvi5907
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/ 19

CODEITUP

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

https://codeitup.in https://codeitup.in
Notes By: Anand Sir
Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

#write a program to store your name in file live.txt


f=open("live.txt","w")
f.write("CODEITUP\nAn Online Learning Platform...\nFrom Basic to
Advanced...")
print("File Created Successfully...")
f.close()

#Accept user input and store in file live.txt


f=open("live.txt","w")
roll=int(input("Enter ROll Number:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
data=str(roll)+"\t"+name+"\t"+str(marks)
f.write(data) Notes By: Anand Sir
print("Data Inserted Successfully...") Author: CBSE CS Python Class 11 & 12
f.close() YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

#Accept user input and store in file live.txt


f=open("live.txt","w")
while True:
roll=int(input("Enter Roll Number:"))
name=input("Enter Name:")
marks=int(input("Enter Marks:"))
data=str(roll)+"\t"+name+"\t"+str(marks)+"\n"
f.write(data)
choice=int(input("\n1->Enter More\n2->Exit\nEnter Your Choice:"))
if choice==2:
break
print("Data Inserted Successfully...")
f.close()

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

f=open("myfile.txt","w")
list1=['Ram\t','Shyam\t','Sita\t','Gita\t']
f.writelines(list1)
print("File Created Successfully...")
f.close()
Notes By: Anand Sir
Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

#Read contents of a file called "myfile.txt"


f=open("myfile.txt","r")
data=f.read(5)
print(data)
data=f.read(5)
print(data)
f.close()

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files


#Read contents of a file called "myfile.txt" #count total number of 'He' he, HE, He, hE
f=open("myfile.txt","r") z=x.split()
x=f.read() count=0
#Count total number of characters========== for i in z:
print("Total Number of Characters=",len(x)) if i.upper()=='TO':
#Count total number of words count+=1
y=x.split() print("Total Number of To=",count)
print("Total Number of words=",len(y)) #Print only those lines that starts from "WE".
#Count total number of lines=============== x=f.readlines()
count=1 print(x)
for i in x:
if i=='\n':
count+=1
print("Total Number of Lines=",count) f.close()
#Count total number of sentences===========
count=0
for i in x:
if i=='.':
count+=1
print("Total number of lines=",count)
#Total vowel and consonants================
v=c=0
for i in x:
if i.isalpha():
if i in 'aeiouAEIOU': Notes By: Anand Sir
v+=1 Author: CBSE CS Python Class 11 & 12
else:
c+=1 YouTube Channel: CODEITUP
print("Total Vowel=",v,"\nTotal COns=",c)
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

#Write a program to read content from a file myfile.txt and print only those lines that starts from "WE".
f=open("myfile.txt","r")
data=f.readlines()
for i in data:
x=i.split()
if x[0].upper()=='WE':
print(i)
f.close()

f=open("myfile.txt")
data=f.readline()
print(data)
data=f.readline()
print(data)
f.close() Notes By: Anand Sir
Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP
codeitup @codeitupyt codeituplearners codeitupyt

CS Class 12 | Python File Handling | Text Files

Notes By: Anand Sir


Author: CBSE CS Python Class 11 & 12
YouTube Channel: CODEITUP

You might also like