0% found this document useful (0 votes)
4 views3 pages

Creating a Python Program to Create and Update

The document provides Python programs for creating and updating records in a binary file using pickle, and for creating and searching employee records in a CSV file using the csv module. The first program allows users to input roll numbers and names, while the second program enables users to input serial numbers, names, and departments, and search for records in the IT department. Both programs demonstrate basic file handling and data manipulation in Python.

Uploaded by

Ridhi Arora
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)
4 views3 pages

Creating a Python Program to Create and Update

The document provides Python programs for creating and updating records in a binary file using pickle, and for creating and searching employee records in a CSV file using the csv module. The first program allows users to input roll numbers and names, while the second program enables users to input serial numbers, names, and departments, and search for records in the IT department. Both programs demonstrate basic file handling and data manipulation in Python.

Uploaded by

Ridhi Arora
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/ 3

#CREATING A PYTHON PROGRAM TO CREATE AND UPDATE/MODIFY

RECORDS IN BINARY FILE

import pickle

def create():

f=open("ridhi.dat","ab")

l=[]

n=int(input("enter no of recs:"))

for i in range(n):

rn=int(input("enter roll no:"))

name=input("enter name:")

l1=[rn,name]

l.append(l1)

pickle.dump(l,f)

f.close()

def update():

f1=open("ridhi.dat","rb+")

while True:

try:

data=pickle.load(f1)

l2=[]

for j in data:

if j[0]==1:

n=input("enter new name:")

l3=[j[0],n]

l2.append(l3)

pickle.dump(l2,f1)

except EOFError:

print("file not found")

f1.close()
create()

update()

#CREATING A PYTHON PROGRAM TO CREATE AND SEARCH EMPLOYEE’S


RECORD IN CSV FILE.

import csv

def create():

f=open("abc.csv","a")

w=csv.writer(f)

n=int(input("enter no of recs:"))

for i in range(n):

sno=int(input("enter serial no:"))

name=input("enter name:")

dept=input("enter department:")

l1=[sno,name,dept]

w.writerow(l1)

f.close()

def search():

f1=open("abc.csv","r")

try:

data=csv.reader(f1)

for i in data:

if str(i[2])=="it dept":

print(i)

except IndexError:

print("out of range")

f1.close()

create()

search()

You might also like