You are on page 1of 9

HOLY PUBLIC

SCHOOL
Academic Year: 2023 -2024

Name of Assignment
PYTHON PROGRAMME AND SQL

Subject: COMPUTER SCIENCE

Report File

Submitted By Summited To
Shreya RAHUL MATHUR
Section: “B” HOD CS
Roll No.:
[1] Create a text file “intro.txt” in python and ask the user to write a single
line of text by user input.

program1():
f = open("intro.txt","w")
text=input("Enter the text:")
f.write(text)
f.close()
program1()

[2] Create a text file “MyFile.txt” in python and ask the user to write
separate 3 lines with three input statements from the user.

def program2():

f = open("MyFile.txt","w")
line1=input("Enter the text:")
line2=input("Enter the text:")
line3=input("Enter the text:")
new_line="\n"
f.write(line1)
f.write(new_line)
f.write(line2)
f.write(new_line
f.write(line3)
f.write(new_line)
f.close()
program2()
[3] Write a program to read the contents of both the files created in the above programs and
merge the contents into “merge.txt”. Avoid using the close() function to close the files.

def program3():
with open("MyFile.txt","r") as f1:
data=f1.read()
with open("intro.txt","r") as f2:
data1=f2.read()
with open("merge.txt","w") as f3:
f3.write(data)
f3.write(data1)
program3()

[4] Write a program to count a total number of lines and count the total number of lines
starting with ‘A’, ‘B’, and ‘C’. (Consider the merge.txt file)

def program5():
with open("merge.txt","r") as f1:
data=f1.readlines()
cnt_lines=0 cnt_A=0 cnt_B=0
cnt_C=0
[5] Count the total number of upper case, lower case, and digits used in
the text file “merge.txt”.

def program4():
with open("merge.txt","r") as f1:
data=f1.read()
cnt_ucase =0
cnt_lcase=0
cnt_digits=0
for ch in data:
if ch.islower():
cnt_lcase+=1
if ch.isupper():
cnt_ucase+=1
if ch.isdigit():
cnt_digits+=1
print("Total Number of Upper Case letters are:",cnt_ucase)
print("Total Number of Lower Case letters are:",cnt_lcase)
print("Total Number of Digit Case letters are:",cnt_digit)

[6] Read the contents of file in reverse order:

def program11():
for i in reversed(list(open("merge.txt","r"))):
print(i.rstrip())
program11()
[7] Find the total occurrences of a specific word from a text file:

def program6(): cnt = 0


word_search = input("Enter the words to search:") with open("merge.txt","r") as f1:
for data in f1:
words = data.split() for word in words:
if (word == word_search): cnt+=1
print(word_search, "found ", cnt, " times from the file")
program6()

[8] Read a text file and display the number of vowels/ consonants/
uppercase/lowercase characters in the file.

f=open("MyFile.txt") d=f.read()
v=0 c=0 u=0 l=0
for i in d:

if i.isupper():

u+=1
elif i.islower():
l+=1
if i.lower() in 'aeiou':
v+=1
elif i.isspace():
pass
elif i.lower() not in 'aeiou':
[9] Read a text file line by line and display each word separated by a
#.

f=open("MyFile.txt") d=f.read()
s=''
for i in d:
s+=i.replace(' ','#')
print(s)
f.close()

[10] Replace multiple spaces with single space in a text file.


def program12():
f1 = open("merge.txt","rt")
f2 = open("merge1.txt","wt")
for line in f1:
f2.write(' '.join(line.split()))
f1.close()
f2.close()
program12()
[11] Append the contents in entered by the user in the text file:

def program10():
text = input("Enter text to append in the file:")
with open("merge.txt","a") as f1:
f1.write(text)
program10()

[12] WAP to read content of a binary file and point only those who have got marks above 90
percent
import pickle
def display student():
f=open("student.dat","rb")
try:
while true:
data=pickle.load(f)
if data[2]>90:
print(data)
except:
f.close()
[13] Write a program to know the cursor position and print the text according
to below-given specifications:
1. Print the initial position
2. Move the cursor to 4th position
3. Display next 5 characters
4. Move the cursor to the next 10 characters
5. Print the current cursor position
6. Print next 10 characters from the current cursor position
def program9():
f = open("merge.txt","r")
print(f.tell())
f.seek(4,0)
print(f.read(5))
f.seek(10,0)
print(f.tell())
print(f.seek(7,0))
print(f.read(10))

[14] WAP to delete a file named student in binary and rename it


import pickle
def remove(rid):
f=open("student.dat","rb")
g=open("student.dat","wb")
try:
while true:
data=pickle.load(f)
if data[0]!=rid:
pickle.dump(data,g)
except:
f.close()
g.close()
os.remove("student.dat")
os.rename("temp.dat","student.dat")
[15] WAP to read the data from a file and write it in another file by removing

/n

obr=open('abc.txt','r')
obw=open('temp.txt','w')
st=" "
while(st):
st=obr.readline()
st=st.rstrip('/n')
obw.write(st)
obr.close() obw.close()

You might also like