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

Python Programs for File Handling and Stack

The document contains four Python programs demonstrating various functionalities: creating and updating a binary file with student details, writing a multi-line string to a binary file, counting characters in a text file, and implementing a stack using a list. Each program includes code snippets and sample outputs. The programs cover file handling, string manipulation, and data structure operations.

Uploaded by

cspro4158
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)
12 views5 pages

Python Programs for File Handling and Stack

The document contains four Python programs demonstrating various functionalities: creating and updating a binary file with student details, writing a multi-line string to a binary file, counting characters in a text file, and implementing a stack using a list. Each program includes code snippets and sample outputs. The programs cover file handling, string manipulation, and data structure operations.

Uploaded by

cspro4158
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

PROGRAM NUMBER : 1

[Link] A BINARY FILE WITH (NAME, ROLL NUMBER AND MARKS) INPUT THE ROLL NUMBER
AND UPDATE THE MARKS

PROGRAM

import pickle # Import pickle module for binary file handling

# Open binary file in write mode

f = open("[Link]", "wb")

# Take student details and store in binary file

[Link]([input("Enter Name: "),int(input("Enter Roll: ")),int(input("Enter Marks: "))], f)

[Link]() # Close the file after writing

# Open file in read and write mode

f = open("[Link]", "rb+")

# Read student record from file

d = [Link](f)

# Ask roll number to update marks

if d[1] == int(input("Enter Roll to update: ")):

# Update marks

d[2] = int(input("Enter New marks: "))

# Move file pointer to beginning

[Link](0)

# Write updated record back to file

[Link](d, f)

[Link]() # Close the file

OUTPUT:

Enter Name: hari

Enter Roll: 12

Enter Marks: 499

Enter Roll to update: 12

Enter New marks: 500


PROGRAM NUMBER:2
[Link] A PYTHON PROGRAM TO CREATE A BINARY FILE namely [Link] AND WRITE A
STRING HAVING TWO LINES IN IT.

import pickle # Import pickle module for binary file handling

# Open binary file in write mode

f = open("[Link]", "wb")

# Create a string with two lines

text = "This is first line\nThis is second line"

# Write the string into the binary file

[Link](text, f)

# Close the file

[Link]()

OUTPUT:
PROGRAM NUMBER :3
[Link] A PROGRAM TO READ A TEXT FILE AND DISPLAY THE NUMBER OF (“VOWELS,
CONSONANTS ,UPPERCASE ,LOWERCASE” )CHARACTERS IN THE FILE

PROGRAM

# Open the text file in read mode

f = open("[Link]", "r")

# Read the complete file content

text = [Link]()

# Close the file

[Link]()

# Initialize counters

v=c=u=l=0

# Check each character in the file

for ch in text:

# Count uppercase letters

if [Link]():

u += 1

# Count lowercase letters

if [Link]():

l += 1

# Check only alphabet characters

if [Link]():

# Check vowels

if [Link]() in "aeiou":

v += 1

# Otherwise consonants

else:

c += 1
# Display the result

print("Vowels:", v)

print("Consonants:", c)

print("Uppercase:", u)

print("Lowercase:", l)

OUTPUT:

Vowels: 10

Consonants: 15

Uppercase: 3

Lowercase: 22

PROGRAM NUMBER : 4
4. WRITE A PROGRAM TO IMPLEMENT A STACK USING LIST []

stack = [] # Create empty stack using list

while True:

print("\n1. Push")

print("2. Pop")

print("3. Peek")

print("4. Display")

print("5. Exit")

ch = int(input("Enter your choice: "))

if ch == 1:

item = input("Enter element: ")

[Link](item) n# Push operatio

print("Element pushed")
elif ch == 2:

if stack == []:

print("Stack is empty")

else:

print("Popped element:", [Link]()) # Pop operation

elif ch == 3:

if stack == []:

print("Stack is empty")

else:

print("Top element:", stack[-1]) # Peek operation

elif ch == 4:

print("Stack elements:", stack)

elif ch == 5:

break

else:

print("Invalid choice")

OUTPUR :

1. Push

2. Pop

3. Peek

4. Display

5. Exit

Enter your choice: 5

You might also like