[go: up one dir, main page]

0% found this document useful (0 votes)
15 views12 pages

file handling programs

lolol

Uploaded by

ASHWIN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

file handling programs

lolol

Uploaded by

ASHWIN KUMAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

#Program-1:

#Count the number of characters from a file. (Include white spaces)

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:

#Count number of vowels in a text file.

fin=open("sample.txt",'r')

str=fin.read()

count=0

for i in str:

if i=='a' or i=='e' or i=='i' or i=='o' or i=='u':

count=count+1
print(count)

fin.close( )

#program - 4

#to count the number of alphabets present in a text file.

f1=open("sample.txt","r")

s=f1.read()

countA=0

for ch in s:

if ch.isalpha():

countA+=1

print("Number of Alphabets : ",countA)

#Program-5:

#Count the number of words in a file.

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:

#Count the number of ‘is’ word in a text file.

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

#display those words start with “a” in a text file.

f1=open("sample.txt","r")

s=f1.read()

print("All Data From File :\n",s)

words=s.split()

print("All data in words: \n",words,", \n Number of Words: ",len(words))

countA=0

for word in words:

if word[0]=='a':

countA+=1

print("Number of words start with 'a' :",countA)

#program - 8

#to count the user define word present in a text file

f1=open("sample.txt","r")

s=f1.read()

sword=input("Enter Your search Word: ")

print("word to search: ",sword)

print("All Data From File :\n",s)

words=s.split()

count=0

print("All data in words: \n",words,", \n Number of Words: ",len(words))


for word in words:

if word==sword:

count+=1

print("This %s comes %d time"%(sword,count))

#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))

for word in 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))

for word in words:

if word[-1].isupper()==True:

count+=1

print(count)
#program-11

#to display those words, which are less than 4 characters.

f1=open("sample.txt","r")

s=f1.read()

print(s)

count=0

words=s.split()

print(words," , ",len(words))

for word in words:

if len(word)==4:

print(word)

count+=1

print(count)

#Program-12:

#Count number of lines in a text file.

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')

n=int(input("How many records you want to write in a file ? :"))

for i in range(n):

print("Enter details of record :", i+1)

title=input("Enter the title of book : ")

price=input("Enter price of the book: ")

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=[]

n=int(input("How many records you want to write in a file ? :"))

for i in range(n):

print("Enter details of record :", i+1)

title=input("Enter the title of book : ")

price=input("Enter price of the book: ")

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:

with open("sample22.txt", "w") as f1:

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

print('Total names beginning from "S" are ',count)

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)

ch=input("want to enter more records?")

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:

print("Reached End of file: No more records")

def update():

with open("binaryfile.dat","rb+") as f:

a=int(input("Enter rollno to be updated"))

found=0

pos=0

while True and found==0:

rec=pickle.load(f)

print(rec)

if rec[0]==a:

f.seek(pos)

rec[2]=float(input("Enter updated percentage"))

pickle.dump(rec,f)

found=1

else:

pos=f.tell()

print(pos)

def search():

with open("binaryfile.dat","rb") as f:

a=int(input("Enter rollno to search"))

found=0
pos=0

while True and found==0:

rec=pickle.load(f)

if rec[0]==a:

print(rec)

found=1

else:

pos=f.tell()

print()

print("to write record into the binary file")

print()

write()

print()

print("to search record in the binary file")

print()

search()

print()

print("to update record in the binary file")

print()

update()

print()

print("to read record from the binary file")

print()

read()
'''import csv

def emp_write(ename,eno): # to write/add data into the CSV file

f=open("Employee.csv",'a')

fw=csv.writer(f)

fw.writerow([ename,eno])

f.close()

def emp_read(): #to read data from CSV file

with open("Employee.csv",'r') as f:

fr=csv.reader(f)

for row in fr:

print(row)

f.close()

emp_write("Arthi",111)

emp_write("Vicky",222)

emp_write("Ramya",333)

emp_read()

'''

import csv # Line 1

def addCsvFile(UserName,PassWord):

f=open('sample.csv','a') # Line 2

newFileWriter = csv.writer(f)

newFileWriter.writerow([UserName,PassWord])

f.close()

#csv file reading code

def readCsvFile(): # to read data from CSV file

with open('sample.csv','r') as newFile:

newFileReader = csv.reader(newFile) # Line 3

for row in newFileReader:

print (row)

newFile.close() # Line 4
addCsvFile("Arjun","123@456")

addCsvFile("Arunima","aru@nima")

addCsvFile("Frieda","myname@FRD")

readCsvFile() #Line 5

You might also like