0% found this document useful (0 votes)
9 views6 pages

Cs Programs Part2

Uploaded by

sleepyguy1609
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)
9 views6 pages

Cs Programs Part2

Uploaded by

sleepyguy1609
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/ 6

Write a Python code to find the size of the file in bytes, the number of lines, number of words

and no. of character.

no_of_lines = 0

words = 0

letters = 0

for line in open("Myfile.txt"):

no_of_lines += 1

for i in line:

if i.isspace()==False:

letters += 1

l=line.split()

words=words+len(l)

f=open("Myfile.txt")

print("The Content in File :\n\n",f.read())

print("Lines:", no_of_lines)

print("Words:", words)

print("Letters:", letters)
# Create a binary file with the name and roll number. Search for a given roll number and
display the name, if not found display appropriate message.

import pickle
def Readrecord():
with open ('StudentRecordl.dat','rb') as Myfile:
print("\n DISPLAY STUDENTS DETAILS ")
print("\n Roll No.",'\t','Name',end='')
print()
while True:
try:
rec=pickle.load(Myfile)
print(rec['SROLL'],'\t\t' ,rec['SNAME'])
except EOFError:
break
def insert():
Myfile = open('StudentRecordl.dat','ab')
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
srecord={"SROLL":sroll,"SNAME":sname}
pickle.dump(srecord,Myfile)
Myfile.close()
def SearchRecord(roll):
with open ('StudentRecordl.dat','rb') as Myfile:
while True:
try:
rec=pickle.load(Myfile)
if rec['SROLL']==roll:
print("Roll NO:",rec['SROLL'])
print("Name:",rec['SNAME'])
break
except EOFError:
print(" Record not find ")
print("Try Again ")
break
def main():
while True:
print('\nYour Choices are: \n')
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Search Records (By Roll No)')
print('0.Exit (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
insert()
elif ch==2:
Readrecord()
elif ch==3:
r=int(input(" Enter a Rollno to be Search: "))
SearchRecord(r)
else:
break
main( )
OUTPUT
# Create a binary file with roll number, name and marks. Input a roll number and update
details. Create a binary file with roll number, name and marks. Input a roll number and
update details.

import pickle
def Readrecord():
with open ('StudentRecord.dat','rb') as Myfile:
print("\n DISPALY STUDENTS DETAILS ")
print("\n Roll No.",'\t' ,'Name','\t',end='')
print('\tPercetage','\t','Remarks')
while True:
try:
rec=pickle.load(Myfile)
print(rec['SROLL'],'\t\t ' ,rec['SNAME'],'\t ',end="")
print(rec['SPERC'],'\t\t',rec['SREMARKS'])
except EOFError:
break
def Input_data():
n=int(input("How many records you want to create :"))
Myfile= open ('StudentRecord.dat','ab')
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
sperc=float(input("Enter Percentage: "))
sremark=input("Enter Remark: ")
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc, "SREMARKS":sremark}
pickle.dump(srecord,Myfile)
print("Successfully stored")
Myfile.close()
def Modify (roll):
with open ('StudentRecord.dat','rb') as Myfile:
newRecord=[]
while True:
try:
rec=pickle.load(Myfile)
newRecord.append(rec)
except EOFError:
break
found=0
for i in range(len(newRecord)):
if newRecord[i]['SROLL']==roll:
name=input("Enter Name: ")
perc=float(input("Enter Percentage: "))
remark=input("Enter Remark: ")
newRecord[i]['SNAME']=name
newRecord[i]['SPERC']=perc
newRecord[i]['SREMARKS']=remark
found=1
else:
found=0
if found==0:
print("Record not found")
with open ('StudentRecord.dat','wb') as Myfile:
for j in newRecord:
pickle.dump(j,Myfile)
print('Successfully Updated')
def main():
while True:
print('\nYour Choices are: ')
print('1.Insert Records')
print('2.Dispaly Records')
print('3.Update Records')
print('O.Exit (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input_data()
elif ch==2:
Readrecord()
elif ch==3:
r =int(input("Enter a Rollno to be update: "))
Modify(r)
else:
break
main()

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy