file handling programs
file handling programs
fin=open("sample.txt",'r')
str=fin.read()
count_char=0
for i in str:
count_char=count_char+1
print(count_char)
fin.close( )
#program-2:
#Count the number of characters from a file. (Don’t count white spaces)
fin=open("sample.txt",'r')
str=fin.read()
print(str)
L=str.split()
print(L)
count_char=0
for i in L:
count_char=count_char+len(i)
print(count_char)
fin.close( )
#Program-3:
fin=open("sample.txt",'r')
str=fin.read()
count=0
for i in str:
count=count+1
print(count)
fin.close( )
#program - 4
f1=open("sample.txt","r")
s=f1.read()
countA=0
for ch in s:
if ch.isalpha():
countA+=1
#Program-5:
fin=open("sample.txt",'r')
str=fin.read()
L=str.split()
count_words=0
for i in L:
count_words=count_words+1
print(count_words)
fin.close( )
#Program-6:
fin=open("sample.txt",'r')
str=fin.read()
L=str.split()
count=0
for i in L:
if i=='school':
count=count+1
print(count)
fin.close( )
#program - 7
f1=open("sample.txt","r")
s=f1.read()
words=s.split()
countA=0
if word[0]=='a':
countA+=1
#program - 8
f1=open("sample.txt","r")
s=f1.read()
words=s.split()
count=0
if word==sword:
count+=1
#Program-9
# to read and count Words start with Upper case character from text file
f1=open("sample.txt","r")
s=f1.read()
print(s)
words=s.split()
print(words,", ",len(words))
if word[0].isupper()==True:
count+=1
print(count)
#program-10
#to read and count Words end with upper case from text file
f1=open("sample.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words," , ",len(words))
if word[-1].isupper()==True:
count+=1
print(count)
#program-11
f1=open("sample.txt","r")
s=f1.read()
print(s)
count=0
words=s.split()
print(words," , ",len(words))
if len(word)==4:
print(word)
count+=1
print(count)
#Program-12:
fin=open("sample.txt",'r')
str=fin.readlines()
count_line=0
for i in str:
count_line=count_line+1
print(count_line)
fin.close( )
#Program: 13
#Program to take the details of book from the user and write the record in text file.
fout=open("sample1.txt",'w')
for i in range(n):
record=title+" , "+price+"\n"
fout.write(record)
fout.close( )
#Program: 14
#Program to take the details of book from the user and write the record in text file.
fout=open("sample1.txt",'w')
list1=[]
for i in range(n):
record=title+" , "+price+"\n"
list1.append(record)
fout.writelines(list1)
fout.close( )
#Program-15:
#to read from 1 file and write into another file
with open("sample.txt") as f:
for line in f:
f1.write(line)
'''import pickle
def write():
with open("NAMES.DAT","wb") as f:
for i in range(5):
pickle.dump("asdfghjklmnbghtyuijh",f)
write()
def SCOUNT():
count=0
with open('NAMES.DAT','rb') as f:
s=f.read(20)
s=s.decode()
print(s)
f.seek((5-1)*20)
print(f.tell())
s=f.read(20)
s=s.decode()
print(s)
SCOUNT()
def SCOUNT():
s=' '
count=0
with open('NAMES.DAT','rb') as f:
while(s):
s=f.readline()
s=s.decode()
print(s)
if len(s)!=0:
if s[0].lower()=='s':
count+=1
SCOUNT()
'''
import pickle
def write():
with open("binaryfile.dat","wb") as f:
while True:
roll=int(input("Enter rollno:"))
name=input("Enter Name:")
per=float(input("Enter Percentage:"))
rec=[roll,name,per]
pickle.dump(rec,f)
if ch=='n' or ch=='N':
break;
def read():
with open("binaryfile.dat","rb") as f:
try:
while True:
rec=pickle.load(f)
print(rec)
print()
except EOFError:
def update():
with open("binaryfile.dat","rb+") as f:
found=0
pos=0
rec=pickle.load(f)
print(rec)
if rec[0]==a:
f.seek(pos)
pickle.dump(rec,f)
found=1
else:
pos=f.tell()
print(pos)
def search():
with open("binaryfile.dat","rb") as f:
found=0
pos=0
rec=pickle.load(f)
if rec[0]==a:
print(rec)
found=1
else:
pos=f.tell()
print()
print()
write()
print()
print()
search()
print()
print()
update()
print()
print()
read()
'''import csv
f=open("Employee.csv",'a')
fw=csv.writer(f)
fw.writerow([ename,eno])
f.close()
with open("Employee.csv",'r') as f:
fr=csv.reader(f)
print(row)
f.close()
emp_write("Arthi",111)
emp_write("Vicky",222)
emp_write("Ramya",333)
emp_read()
'''
def addCsvFile(UserName,PassWord):
f=open('sample.csv','a') # Line 2
newFileWriter = csv.writer(f)
newFileWriter.writerow([UserName,PassWord])
f.close()
print (row)
newFile.close() # Line 4
addCsvFile("Arjun","123@456")
addCsvFile("Arunima","aru@nima")
addCsvFile("Frieda","myname@FRD")
readCsvFile() #Line 5