You are on page 1of 18

WEEK-9

Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

1.Aim : Write a python program to write contents into a file.

Code :

fp=open("demo.txt","w")
L="Hi user! Happy learning\n"
fp.write(L)
fp.write("Welcome CSE")
fp.close()

fp=open("demo.txt","r")
text=fp.read()
print(text)
fp.close()
Output :

Hi user! Happy learning


Welcome CSE

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

2.Aim : Write a python program to insert 1 to 20 numbers into a file.

Code :

fp=open("num.txt","w")
for i in range(1,21):
i=str(i)
a=fp.write(i)
fp.write(" ")
fp.close()

fp=open("num.txt","r")
a=fp.read()
print(a)
fp.close()
Output :

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

3.Aim : Write a python program to insert 50 numbers randomly in the


range of 500 to 1000.

Code :

import random
fp=open("rand.txt","w")
for i in range(1,51):
i=random.randint(500,1000)
i=str(i)
a=fp.write(i)
fp.write(" ")
fp.close()

fp=open("rand.txt","r")
a=fp.read()
print(a)
fp.close()
Output :

720 652 827 530 958 560 610 631 510 636 534 927 570 629 947 530 595 830 899 799 553
585 504 664 575 850 824 578 566 984 725 918 723 928 845 652 611 753 516 739 944 881
765 745 710 970 698 950 944 548

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

4.Aim : Imagine there is a file consisting of five subject marks. Now write
a program to read contents of the file and calculate sum and average.

Code :

fp=open("marks.txt","r")
x=fp.readline()
num=int(x)
sum=0
print("The marks of ",num,"subjects : ",end="")
for i in range(num) :
y=int(fp.readline())
print(y,end=" ")
sum=sum+y
print()
print("The sum is : ",sum)
avg=sum/5
print("The average is ",avg)
Output :

The marks of 5 subjects : 100 100 100 100 100


The sum is : 500
The average is 100.0

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

5.Aim : Write a python program to insert message at end of the file(Hello


this is Python Programming) demo1.txt which already contain content
“Hi! This is Computer Science”.

Code :

fp=open("demo1.txt","a")
fp.write("Hello This is python programming.")
fp.close()

fp=open("demo1.txt","r")
a=fp.read()
print(a)
fp.close()
Output :

Hi! This is Computer Science.


Hello This is python programming.Hello This is python programming.

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

6.Aim : Imagine there is a file consisting of four lines. Out of four , two
lines start with capital letter. Now write a python program to copy
contents of first file to another file, only the lines start with capital letters.

Code :

fp1=open("demo2.txt","r")
fp2=open("demo3.txt","w")
for line in fp1 :
if line[0].isupper() :
fp2.write(line)
fp2.close()
fp1.close()
print("The first file data : ")
fp=open("demo2.txt","r")
a=fp.read()
print(a)
fp.close()
print("The new file data : ")
fp=open("demo3.txt","r")
a=fp.read()
print(a)
fp.close()
Output :

The first file data :


Hello this is python.
python is good.
But sometimes it is tough.
overall it is ok.
5k

The new file data :


Hello this is python.
But sometimes it is tough.

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

7.Aim : Write a python program to implement w+ and seek methods.

Code :

fo = open("demo6.txt","w+")
print(fo.readline())
fo.write("The name Mahatma Gandhi is now one of the most universally recognized on
earth.")
fo.seek(20)
print(fo.tell())
print(fo.readline())
fo.close()
fo = open("demo6.txt","r")
fo.seek(10)
print(fo.tell())
print(fo.readline())
fo.close()
Output :

20
dhi is now one of the most universally recognized on earth.
10
ahatma Gandhi is now one of the most universally recognized on earth.

Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557

8.Aim : Imagine there is a file with 5 lines of different size. Now write a
program to display in the following format.
Line No : __
(Content)
Size : __ Characters
--------------------
The Longest line is
(Content)
Size : __ Characters

Code :

fp1=open("sample.txt","r")
long=""
L=0
count=0
for line in fp1 :
count=count+1
print("Line No. : ",count)
print(line)
print("Size : ",len(line),"characters")
print("--------------")
if(len(line)>len(long)) :
long=line
L=line
fp1.close()
print("The longest line is ")
print(L)
print("Size : ",len(L),"characters")
Output :

Line No. : 1
Python is a high-level, general-purpose programming language.

Size : 63 characters
--------------
Line No. : 2
Its design philosophy emphasizes code readability with the use of significant indentation.

Size : 91 characters
--------------
Line No. : 3
Python is dynamically-typed and garbage-collected.

Size : 51 characters
--------------
Line No. : 4
It supports multiple programming paradigms, including structured (particularly
procedural), object-oriented and functional programming.

Size : 136 characters


--------------
Line No. : 5
It is often described as a "batteries included" language due to its comprehensive standard
library.

Size : 100 characters


--------------
The longest line is
It supports multiple programming paradigms, including structured (particularly
procedural), object-oriented and functional programming.

Size : 136 characters

Inference :
END OF THE WEEK : 9

Concepts Covered :

1. Read contents from file.


2. Write contents to file.
3. w+ and seek methods.
4. Append contents to a file.

GRADE : _______________
Signature of Lab-Incharge : _________________________

You might also like