0% found this document useful (0 votes)
37 views7 pages

Chetan 2 File Handling and Stack

Uploaded by

K.D. computer
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)
37 views7 pages

Chetan 2 File Handling and Stack

Uploaded by

K.D. computer
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/ 7

File handling program and stack

program:-
1. W.r.t program to append contents to file
using  write()  method.

F=open("data.dat","a")

while(True):
id=input("Enter Id:")
name=input("Enter Name:")
salary=input("Enter Salary:")

data="{0},{1},{2}\n".format(id,name,salary)
F.write(data)

ch=input("Continue y/n?")

if(ch=="n"):break
F.close()

Output:-
Enter Id:10032
Enter Name:John Does
Enter Salary:45000
Continue y/n?y
Enter Id:10323
Enter Name:Ram
Enter Salary:50000
Continue y/n?n

File Contents (data.dat):


10032,John Doe,45000
10323,Ram,50000

2. Python program to check if the record is


present in the file using its id.

F=open("data.dat","r")

id=input("Enter Id:")

found=False

while(True):
data=F.readline()
if(data==""):
break

DL=data.split(",")
if(DL[0]==id):
DL[2]=DL[2].rstrip("\n")
DL.append(int(DL[2])*20/100)
print(DL)
found=True
break
if(not found):
print("Record Not Found")

F.close()
Output:-
Enter Id:10323
['10323', 'Ram', '50000', 10000.0]

3. W.r.t  count total number of uppercase and lowercase


characters in file in Python?

# Start of try block


try:
#Counter for characters...
upperCount = 0
lowerCount = 0

F=open("file.dat","r")
while(True):
data=F.read(1)
if(data==""):
break
if (data.isupper()):
upperCount = upperCount + 1
elif (data.islower()):
lowerCount = lowerCount + 1

print(data,end='')

print("Total Upper Case:",upperCount)


print("Total lower Case:",lowerCount)
except FileNotFoundError as e:
print(e)
finally:
F.close()
Output:-
Total Upper Case: 5
Total lower Case: 24

4. Program to check a file's status in file handling


in Python

# main method
def main():
# Opening the file
fileObject = open("file.dat","rb")

# Printing object's status


print("Name of the File : ",fileObject.name)
print("Closed or Not : ",fileObject.closed)
print("Opening Mode : ",fileObject.mode)

fileObject.close()

print("")
print("Closed or Not : ",fileObject.closed)

if __name__=="__main__":main()

Output:-
Name of the File : file.dat
Closed or Not : False
Opening Mode : rb

Closed or Not : True


5. W.r.t Program to delete a file in python

# importing os library...
import os

# main method
def main():
# removing file using remove method
os.remove("data.txt")
print("File 'data.txt' is deleted!")

if __name__=="__main__":main()

Output:-

File 'data.txt' is deleted!

6.Write a python program to implement a stack using a


list data structure

stack = ["One", "Two", "Three"] stack.append ("Four")


stack.append ("Five"')
print ("Print the elements in the stack", stack)
print ("pop 1", stack.pop () )
print ("Afte performing first pop", stack)
print ("pop 2", stack.pop () )
print ("Afte performing second pop", stack)

Output:-

Print the elements in the stack ('One', 'Iwo', 'Three',


'Four', 'Five') pop 1 Five
Afte performing first pop ('One', 'Two', 'Three', 'Four')
pop 2 Four
Afte performing second pop ('One', 'Iwo', 'Three')

7.Write a python program to queue the list

#Author Vijaya Kumar Chinchala


mmm Python code to demonstrate Implementing
Queue using list
grueue = One",”Two","Three"]
queue. append ("Four")
queue. append ("Five")
print ("Print the elements in the stack", queue)
# Removes the first item
print ("pop I", queue.pop (0))
print ("Afte performing first pop", queue)
# Removes the first item
print ("pop 2", queue.pop (0))
print ("After performing second pop", queue)

Output:-
Print the elements in the stack ('One', "Two', 'Three',
'Four''Five')
pop 1 One
After performing first pop ['Two', 'Three', 'Four',
'Five']
pop 2 Two
After performing second pop ['Three', 'Four', 'Five']

You might also like