Professional Documents
Culture Documents
SCHOOL
Academic Year: 2023 -2024
Name of Assignment
PYTHON PROGRAMME AND SQL
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)
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:
[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()
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))
/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()