You are on page 1of 7

PRACTICAL PROGRAMS

1.A binary file “Book.dat” has


structure [BookNo, Book_Name, Author, Price].Write a user defined
function CreateFile() to input data for a record and add to Book.dat .
import pickle
def createfile():
fobj=open("Book.dat","ab")
BookNo=int(input("Enter Book Number : "))
Book_name=input("Enter book Name :")
Author = input("Enter Author name: ")
Price = int(input("Price of book : "))
rec=[BookNo, Book_name ,Author, Price]
pickle.dump(rec, fobj)
fobj.close()
createfile()
print(“File created…”)
2.Create a file write.txt and write data to the file.

3. Program to read and display those lines from file that start with alphabet
‘T’.
file1=open("write.txt","r")
count=0
str1=file1.readlines()
print(str1)
for i in str1:
if i[0]=='T':
print (i)
file1.close()

4. Program to count number of characters in data file write.txt.


file1=open("write.txt","r")
ch=" "
count=0
while ch:
ch=file1.read(1)
count+=1
print("Number of characters=",count)
file1.close()
5.Program to write data in a csv file student.csv.
import csv
fh=open("d:\student.csv","w")
stuwriter=csv.writer(fh)
stuwriter.writerow([1,'aman',50])
stuwriter.writerow([2,'Raman',60])
fh.close()
6.Program to read and display data from a csv file student.csv.
import csv
fh=open("d:\student.csv","r")
stureader=csv.reader(fh)
for rec in stureader:
print(rec)
fh.close()
7. Write a function bwrite() to write list of three names in a binary file?

8.Write a program to append details of one student into stud.dat


import pickle
stu={ }
f1=open(‘stud.dat','ab')
ans='y'
while ans=='y':
rno=int(input("enter rno"))
name=input("enter name")
stu['Rollno']=rno
stu['Name']=name
pickle.dump(stu,f1)
ans=input("want to append more?y/n...")
f1.close()
9.Write a program to read data from binary file stud.dat
import pickle
emp={}
f1=open(‘stud.dat','rb')
try:
while True:#it will become false when the end of file is reached.
emp=pickle.load(f1)
print(emp)
except EOFError:
f1.close()
10.Write a pgm to search if one student rollno is present in the file (stu.dat)or
not.
import pickle
st={}
found=False
fin=open('stu.dat','rb')
searchkey=[1,14]
#read from file
try:
while True:
stu=pickle.load(fin)
if stu['Rollno'] in searchkey:
print(stu)
found=True
except EOFError:
if found==False:
print("No such records found")
else:
print("search successful")
fin.close

11. Counting lower case characters from a text file

12. Counting words from a text file.


13.Write a python program to accept username "Admin" as default argument
and password 123 entered by user to allow login into the system.
def user_pass(password,username="Admin"):
if password=='123':
print("You have logged into system")
else:
print("Password is incorrect!!!!!!")
password=input("Enter the password:")
user_pass(password)
14. Program to sort values in a list.

aList=[10,5,1,3]

print("Original List",aList)

n=len(aList)

for i in range(n-1):

for j in range(0,n-i-1):

if aList[j]>aList[j+1]:

aList[j],aList[j+1]=aList[j+1],aList[j]

print("Sorted List",aList)

15. 6. Write a random number generator that generates random numbers


between 1 and 6 (simulates a dice)

import random

a="y"

while a=="y":
print("random number",random.randint(1,6))

a=input("would you like to continue y/n")

You might also like