Professional Documents
Culture Documents
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557
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 :
Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557
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
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 :
Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557
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 :
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 :
Inference :
Week : 9
Date : 06/12/2022
Developed by : G.Sai Hemanth Kumar
Roll No : 21331A0557
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.
Inference :
END OF THE WEEK : 9
Concepts Covered :
GRADE : _______________
Signature of Lab-Incharge : _________________________