Python Data File Handling Part 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CBSE CLASS 12 CS PYTHON

DATA FILE HANDLING


Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
Notes By: Anand Sir | YouTube Channel: CODEITUP
f=open("ram.txt","w") f=open("ram.txt","w")
name=input("Enter Your Name:") name=["Ram\n","Shyam\n","Sita\n","Gita"]
f.write(name) f.writelines(name)
f.close() f.close()
print("File Created Successfully...") print("File Created Successfully...")

Notes By: Anand Sir | YouTube Channel: CODEITUP


Notes By: Anand Sir | YouTube Channel: CODEITUP
#Read the content of a file called "Student.txt".
f=open("student.txt","r")
x=f.read()
print(x)

f=open("student.txt","r")
x=f.read(7)
print(x)
x=f.read(10)
print(x)
f.close()

#Count total number of characters in a file called


"Student.txt".
f=open("student.txt","r")
x=f.read()
print(x)
print("Total
Notes By: Anand SirNumber of Characters=",len(x))
| YouTube Channel: CODEITUP
#Count total number of vowels and consonants in a
file called "Student.txt".
f=open("student.txt","r")
x=f.read()
print(x)
v=c=0
for i in x:
if i.isalpha():
if i in 'aeiouAEIOU':
v+=1
else:
c+=1
print("Total vowel=",v,"Total consonants=",c)

Notes By: Anand Sir | YouTube Channel: CODEITUP


#Count total number of "We" in a file called
#readline
"Student.txt".
f=open("student.txt","r")
f=open("student.txt","r")
#x=f.readline()
x=f.read()
#print(x)
data=x.split()
#x=f.readline()
count=0
#print(x)
for i in data:
x=f.readline(5)
z=i.upper()
print(x)
if z=='WE':
f.close()
count+=1
print("Total Number of WE=",count)

Notes By: Anand Sir | YouTube Channel: CODEITUP


f=open("student.txt","r")
x=f.readlines()
print(x)

f=open("student.txt","r")
x=f.readlines()
print("Total Number of Lines=",len(x))
print(x)

Notes By: Anand Sir | YouTube Channel: CODEITUP

You might also like