0% found this document useful (0 votes)
7 views5 pages

Python Programs for File Handling and Stacks

Uploaded by

griesh.6g
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)
7 views5 pages

Python Programs for File Handling and Stacks

Uploaded by

griesh.6g
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

1.

Write a Python Program to Create a binary file with Empno, Name, Salary, then read and display
their details on screen.

Aim:

To Write a Python Program to Create a binary file with Empno, Name, Salary, then read
and display their details on screen.

Source Code:
import pickle
def Write():
F=open("[Link]",'ab')
D={}
n=int(input("Enter how many employee details you want to store:"))
for i in range(n):
D['emp no']=int(input("Enter the employee number:"))
D['emp name']=input("Enter the employee name:")
D['Salary']=int(input("Enter the employee salary:"))
[Link](D,F)
[Link]()

def Read():
F=open("[Link]",'rb')
try:
while True:
s=[Link](F)
print(s)
except:
[Link]()

Write()
Read()

Output:

Result: The above program is executed successfully and the output is shown.
2. Create a binary file with roll number, name and marks. Input a roll number and update
the marks.

Aim: To write a python program to create a binary file with roll number, name and marks.
Input a roll number and update the marks.
Source code:
import pickle
sdata={}
totals=int(input("Enter the [Link] students"))
a=open(r"C:\Users\HP\Desktop\[Link]","wb")
for i in range(totals):
sdata["Roll no"]=int(input("Enter roll no:"))
sdata["Name"]=input("Enter name:")
sdata["Marks"]=float(input("Enter marks:"))
[Link](sdata,a)
sdata={}
[Link]()
found=False
rollno=int(input("Enter roll no to update:"))
a=open(r"C:\Users\HP\Desktop\[Link]","rb+")
try:
while True:
pos=[Link]()
sdata=[Link](a)
if(sdata["Roll no"]==rollno):
sdata["Marks"]=float(input("Enter new marks:"))
[Link](pos)
[Link](sdata,a)
found=True
except EOFError:
if found==False:
print("Roll number not found")
else:
print("Student marks updated successfully")
[Link]()
Output:

Result: The above program is executed successfully and the output is shown.
3. Create a CSV file by entering user-id and password, read and search the password for
given user- id.

Aim: To write a program to create a CSV file by entering user-id and password, read and
search the password for given user- id.
Source Code:
import csv
with open("[Link]", "w") as obj:
fileobj = [Link](obj)
[Link](["User Id", "password"])
while(True):
user_id = input("enter id: ")
password = input("enter password: ")
record = [user_id, password]
[Link](record)
x = input("press Y/y to continue and N/n to terminate the program\n")
if x in "Nn":
break
elif x in "Yy":
continue
with open("[Link]", "r") as obj2:
fileobj2 = [Link](obj2)
given = input("enter the user id to be searched\n")
for i in fileobj2:
next(fileobj2)
if i[0] == given:
print(i[1])
break

Output:

Result: The above program is executed successfully and the output is shown.
4. Write a python program using function POP(Arr), where Arr is a stack implemented by a list of
numbers and the function should return the value deleted from the stack.
Aim:
To write a python program using function POP(Arr), where Arr is a stack implemented by a list of
numbers and a function that returns the value deleted from the stack.
Source code:
def isEmpty(Arr):
if len(Arr)==0:
return True
else:
return False
def push(Arr,item):
[Link](item)
top=len(Arr)-1
def pop(Arr):
if isEmpty(Arr):
return 'Underflow occurs'
else:
val=[Link]()
if len(Arr)==0:
top=None
else:
top=len(Arr)-1
return val
def show(Arr):
if isEmpty(Arr):
print('no item found')
else:
t=len(Arr)-1
print('(TOP)',end='')
while(t>=0):
print(Arr[t],'<==',end='')
t=t-1
print()
Arr=[]
top=None
while True:
print('****** STACK IMPLEMENTATION USING LIST ******')
print('1: PUSH')
print('2: POP')
print('3: Show')
print('0: Exit')
ch=int(input('Enter choice:'))
if ch==1:
val=int(input('Enter no to push:'))
push(Arr,val)
elif ch==2:
val=pop(Arr)
if val=='Underflow':
print('Stack is empty')
else:
print('\nDeleted item is:',val)
elif ch==3:
show(Arr)
elif ch==0:
print('Bye')
break
Output:

Result: The above program is executed successfully and the output is shown.

You might also like