Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views8 pages

Program7 19

The document contains a series of Python programs that demonstrate various file handling techniques, including reading from and writing to text and binary files. Programs include functionalities such as counting vowels and consonants, copying file contents, counting lines, and updating student records based on specific criteria. Each program is accompanied by its aim and code implementation, showcasing practical applications of file operations in Python.

Uploaded by

Seetha Lakshmi
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)
6 views8 pages

Program7 19

The document contains a series of Python programs that demonstrate various file handling techniques, including reading from and writing to text and binary files. Programs include functionalities such as counting vowels and consonants, copying file contents, counting lines, and updating student records based on specific criteria. Each program is accompanied by its aim and code implementation, showcasing practical applications of file operations in Python.

Uploaded by

Seetha Lakshmi
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/ 8

#PROGRAM 7

AIM:

Write a program that reads a text file with line by line and display each word separated by " @ "

Program Code:

file1=open("Program.txt","w+")

file1.write("Hello\n")

file1.write("Universe\n")

file1.write("Globe\n")

file1.write("Python\n")

file1.close()

file1=open("Program.txt")

a=file1.read()

b=a.split()

for i in b:

print(i,end=" @ ")

#PROGRAM 8

AIM:

Write a python program that reads a text file and displays number of vowels, consonants, upper case,
lower case characters and digits.

PROGRAM CODE:

file1=open("Program8.txt","w")

file1.write("Hello\n")

file1.write("Program - 8 to count\n")

file1.write("Data Structure\n")
file1.write("Practical Lab Programs\n")

file1.close()

file1=open("Program8.txt")

a=file1.read()

print(a)

vowels="aeiou"

consonants="bcdfghjklmnpqrstvwxyz"

vow,cons,upper,lower,digit=0,0,0,0,0

for i in a:

if i.isupper():

upper+=1

if i.islower():

lower+=1

if i.isdigit():

digit+=1

b=i.lower()

if b in vowels:

vow+=1

if b in consonants:

cons+=1

file1.close()

print("Vowels = ",vow)

print("Consonants = ",cons)

print("Upper_Case = ",upper)

print("Lower_Case = ",lower)

print("Digits = ",digit)

#PROGRAM 9

AIM:
Write a python program to copy content from one file into another file using functions.

PROGRAM CODE:

def copy():

file1=open("File1.txt")

a=file1.read()

file2=open("File2.txt","w")

for line in a:

file2.write(line)

file1.close()

file2.close()

file1=open("File1.txt","w")

file1.write("Functions\n")

file1.write("File Handling\n")

file1.write("Data Structures\n")

file1.write("Networks\n")

file1.write("Database Management \n")

file1.close()

copy()

file2=open("File2.txt")

b=file2.read()

print(b)

file2.close()

#PROGRAM 10

AIM:
Write a python program using function countlines( ) to count the total number of lines and count_AE( )
to count the number of lines starting with letter “A” or “E” in a text file/

PROGRAM CODE:

def countlines(f):

a=f.readlines()

b=len(a)

return(b)

def count_AE(f):

lines=f.read()

count=0

for line in lines:

if line[0]=='A':

count+=1

if line[0]=="E":

count+=1

return count

file1=open("Story.txt","w")

file1.write("Aliens started landing on Earth\n")

file1.write("Everyone in the city panicked\n")

file1.write("Suddenly the aliens started attacking\n")

file1.write("All people running away\n")

file1.close()

f2=open("Story.txt")

ab=f2.read()

print(ab)

f2.close()

file2=open("Story.txt")

tc=countlines(file2)
print("Total number of lines",tc)

file2.close()

file1=open("Story.txt")

ae=count_AE(file1)

print("Lines starting with the letters A and E",ae)

file1.close()

#PROGRAM 11

AIM:

Write a python program to remove all the lines that contain character ‘I’ in a text file and add it to
another file.

PROGRAM CODE:

f1=open("TextFile.txt","w")

f1.write("Hello\n")

f1.write("Citizen\n")

f1.write("Computer science\n")

f1.write("Summer season\n")

f1.write("Thank You\n")

f1.close()

file1=open("TextFile.txt")

file2=open("Characteri.txt","w")

a=file1.readlines()

for i in a:

if 'i' in i:

file2.write(i)

file1.close()

file2.close()

file2=open("Characteri.txt")

b=file2.read()
print("Content of the file containing the character - i:")

print(b)

#PROGRAM 12

AIM:

A binary file “Student.dat” has structure(Roll_No, Name, Marks). Write a function countrec() in python
that would read content of the file and display the details of those students whose marks is above 75. If
not found, display the appropriate message.

PROGRAM CODE:

import pickle

file1=open("Student.dat","wb")

record=[]

n=int(input("How many data to enter ;"))

for i in range(n):

rollno=int(input("Enter the roll number:"))

name=input("Enter the name:")

marks=int(input("Enter the marks:"))

data=[rollno,name,marks]

record.append(data)

pickle.dump(record,file1)

file1.close()

def countrec():

file2=open("Student.dat","rb")

try:

while True:

a=pickle.load(file2)

for i in a:
if i[2]>75:

print("Details of the student")

print(i)

except:

file2.close()

countrec()

#PROGRAM 13

AIM:

A binary file “Student.dat” has structure (Rollno, Name, Marks). Write a function updaterec() in Python
that would update marks of students who have scored more than 80, will get an additional bonus marks
of 2.

PROGRAM CODE:

import pickle

file1=open("Student.dat","wb")

record=[]

n=int(input("How many data to enter-"))

for i in range(n):

rollno=int(input("Enter the roll number:"))

name=input("Enter the name:")

marks=int(input("Enter the marks:"))

data=[rollno,name,marks]

record.append(data)

pickle.dump(record, file1)

file1.close()

def updaterec():

f1=open("Student.dat","rb")

try:

while True:
a=pickle.load(f1)

for i in a:

if i[2]>80:

i[2]+=2

print("Updated mark details:")

print(i)

except:

f1.close()

updaterec()

You might also like