100% found this document useful (1 vote)
727 views17 pages

Python File Reading and Analysis Programs

The document contains 7 programming exercises involving file handling and data structures in Python. Exercise 1 reads a text file line by line and prints each word separated by '#'. Exercise 2 reads a text file and counts the vowels, consonants, uppercase and lowercase letters. Exercise 3 creates a binary file with student names and roll numbers and searches for a given roll number. Exercise 4 updates marks in the binary student data file. Exercise 5 removes lines containing 'a' from one file and writes them to another. Exercise 6 implements a random number generator that rolls a dice between 1-6. Exercise 7 implements a stack using a Python list.

Uploaded by

Shravan
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
100% found this document useful (1 vote)
727 views17 pages

Python File Reading and Analysis Programs

The document contains 7 programming exercises involving file handling and data structures in Python. Exercise 1 reads a text file line by line and prints each word separated by '#'. Exercise 2 reads a text file and counts the vowels, consonants, uppercase and lowercase letters. Exercise 3 creates a binary file with student names and roll numbers and searches for a given roll number. Exercise 4 updates marks in the binary student data file. Exercise 5 removes lines containing 'a' from one file and writes them to another. Exercise 6 implements a random number generator that rolls a dice between 1-6. Exercise 7 implements a stack using a Python list.

Uploaded by

Shravan
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
  • Exercise 1: Read a Text File Line by Line
  • Exercise 2: Display Number of Characters
  • Exercise 3: Search Data in a Binary File
  • Exercise 4: Update Data in a Binary File
  • Exercise 5: File Operations
  • Exercise 6: Random Number Generator
  • Exercise 7: Implementation of Stack Using List

EX.

NO:1 READ A TEXT FILE LINE BY LINE AND DISPLAY EACH WORD SEPARATED BY ‘#’

AIM

To write a python program to read a text file line by line and display each word separated by a #.

PROGRAM

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

lines=[Link]()

for line in lines:

words=[Link]()

for word in words:

print(word+"#",end="")

print("")

[Link]()
OUTPUT

SAMPLE TEXT FILE

PROGRAM OUTPUT
[Link] READ A TEXT FILE AND DISPLAY THE NUMBER OF
VOWELS/CONSONANTS/UPPERCASE/LOWERCASE CHARACTERS IN THE FILE

AIM

To write a python program to read a text file and display the number of vowels/ consonants/
uppercase/ lowercase characters in the file.

PROGRAM

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

content=[Link]()

vowels=0

consonants=0

lower_case_letters=0

upper_case_letters=0

for ch in content:

if([Link]()):

lower_case_letters+=1

elif([Link]()):

upper_case_letters+=1

ch=[Link]()

if (ch in ['a','e','i','o','u']):

vowels+=1

elif(ch in ['b','c','d','f','g','h','j','k','l','m','n','p','q','r','s','t','v','w','x','y','z']):

consonants+=1

[Link]()

print("Vowels are :",vowels)

print("Consonants :",consonants)
print("Lower_case_letters :",lower_case_letters)

print("Upper_case_letters :",upper_case_letters)
OUTPUT

SAMPLE TEXT FILE

PROGRAM OUTPUT
[Link] SEARCHING DATA IN A BINARY FILE

AIM

To write a python program to create a binary file with name and roll number and to search for a given
roll number and display the name.

PROGRAM
“Create a binary file with name and roll number
import pickle
stud_data={}
list_of_students=[]
no_of_students=int(input("Enter no of Students:"))
for i in range(no_of_students):
stud_data["roll_no"]=int(input("Enter roll no:"))
stud_data["name"]=input("Enter name: ")
list_of_students.append(stud_data)
stud_data={}
file=open("C:\\Users\\dasam\\Desktop\\stud_data.dat","wb")
[Link](list_of_students,file)
print("Data added successfully")
[Link]()

“Search for a given roll number and display the name, if not found display appropriate message.”
import pickle
file=open("C:\\Users\\dasam\\Desktop\\stud_data.dat","rb")
list_of_students=[Link](file)
roll_no=int(input("Enter roll [Link] student to search"))
found=False
for stud_data in list_of_students:
if(stud_data["roll_no"]==roll_no):
found=True
print(stud_data["name"],"found in file.")
if (found==False):
print("No student data found. please try again")
[Link]()

OUTPUT
[Link] UPDATING DATA IN A BINARY FILE

AIM

To write a python program to create a binary file with roll number, name and marks and input a roll
number and update the marks.

PROGRAM

#Create a binary file with roll number, name and marks.


import pickle
student_data={}
no_of_students=int(input("Enter no..of Students to insert in file : "))
file=open("C:\\Users\\dasam\\Desktop\\student_data","wb")
for i in range(no_of_students):
student_data["RollNo"]=int(input("Enter roll no :"))
student_data["Name"]=input("Enter Student Name :")
student_data["Marks"]=float(input("Enter Students Marks :"))
[Link](student_data,file)
student_data={}
[Link]()
print("data inserted Successfully")

#Input a roll number and update the marks.


import pickle
student_data={}
found=False
roll_no=int(input("Enter the roll no to search :"))
file=open("C:\\Users\\dasam\\Desktop\\student_data","rb+")
try:
while True:
pos=[Link]()
student_data=[Link](file)
if(student_data["RollNo"]==roll_no):
student_data["Marks"]=float(input("Enter marks to update"))
[Link](pos)
[Link](student_data,file)
found=True
except EOFError:
if(found==False):
print("Roll no not [Link] try again")
else:
print("Students marks updated Successfully")
[Link]()

# print the updated data


import pickle
student_data={}
file=open("C:\\Users\\dasam\\Desktop\\student_data","rb")
try:
while True:
student_data=[Link](file)
print(student_data)
except EOFError:
[Link]()
OUTPUT
[Link] FILE OPERATIONS

AIM

To write a python program to remove all the lines that contain the character `a' in a file and write it to
another file.

PROGRAM

file=open("C:\\Users\\dasam\\Desktop\\[Link]","r")
lines=[Link]()
[Link]()

file=open("C:\\Users\\dasam\\Desktop\\[Link]","w")
file1=open("C:\\Users\\dasam\\Desktop\\[Link]","w")
for line in lines:
if 'a' in line or 'A' in line:
[Link](line)
else:
[Link](line)
print("All lines that contains 'a' character has been removed in [Link] file")
print("All lines that contains 'a' character has been saved in [Link] file")
[Link]()
[Link]()
OUTPUT
[Link] RANDOM NUMBER GENERATOR

AIM:

To write a random number generator that generates random numbers between 1 and 6.

PROGRAM:

import random
while(True):
choice=input("Enter (r) for roll dice or press any other key to quit")
if(choice!="r"):
break
n=[Link](1,6)
print(n)
OUTPUT
[Link] IMPLEMENTATION OF STACK USING LIST

AIM:

To write a Python program to implement a stack using a list data-structure.

PROGRAM:

def push():

a=int(input("Enter the element which you want to push:"))

[Link](a)

return a

def pop():

if stack==[]:

print("Stack empty....Underflow case....can not delete the element...")

else:

print("deleted element is :",[Link]())

def display():

if stack==[]:

print("Stack empty....no elements in stack..")

else:

for i in range(len(stack)-1,-1,-1):

print(stack[i])

stack=[ ]

print("STACK OPERATIONS")

print("***********************")

choice="y"
while choice=="y":

print("[Link]")

print("[Link]")

print("[Link] ELEMENTS OF STACK")

print("************************************")

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

if c==1:

push()

elif c==2:

pop()

elif c==3:

display()

else:

print("wrong input:")

choice=input("Do you want to continue or not?(y/n) : ")


OUTPUT

EX.NO:1  
READ A TEXT FILE LINE BY LINE AND DISPLAY EACH WORD SEPARATED BY ‘#’ 
 
AIM 
To write a python program to read a te
OUTPUT 
SAMPLE TEXT FILE 
 
PROGRAM OUTPUT
EX.NO:2  
 
READ A TEXT FILE AND DISPLAY THE NUMBER OF  
 
VOWELS/CONSONANTS/UPPERCASE/LOWERCASE CHARACTERS IN THE FILE 
 
AI
print("Lower_case_letters :",lower_case_letters) 
print("Upper_case_letters :",upper_case_letters)
OUTPUT 
SAMPLE TEXT FILE 
 
 
PROGRAM OUTPUT
EX.NO:3 
 
 
SEARCHING DATA IN A BINARY FILE 
AIM 
To write a python program to create a binary file with name and roll numbe
file.close() 
OUTPUT
EX.NO:4 
 
 
UPDATING DATA IN A BINARY FILE 
AIM 
To write a python program to create a binary file with roll number, name an
file.seek(pos) 
               pickle.dump(student_data,file) 
               found=True 
except EOFError:
OUTPUT

You might also like