You are on page 1of 2

1. Create a text file “intro.

txt” in cnt_digits=0
python and ask the user to write a for ch in data:
single line of text by user input. if ch.islower():
cnt_lcase+=1
def program1(): if ch.isupper():
f = open("intro.txt","w") cnt_ucase+=1
text=input("Enter the text:") if ch.isdigit():
f.write(text) cnt_digits+=1
f.close() print("Total Number of Upper
program1() Case letters are:",cnt_ucase)
print("Total Number of Lower
[2] Create a text file “MyFile.txt” Case letters are:",cnt_lcase)
in python and ask the user to write print("Total Number of digits
separate 3 lines with three input
are:",cnt_digits)
statements from the user.
program4()

def program2():
[5] Write a program to count a total
f = open("MyFile.txt","w")
number of lines and count the total
line1=input("Enter the text:")
number of lines starting with ‘A’,
line2=input("Enter the text:")
‘B’, and ‘C’. (Consider the
line3=input("Enter the text:")
merge.txt file)
new_line="\n"
def program5():
f.write(line1)
with open("merge.txt","r") as
f.write(new_line)
f1:
f.write(line2)
data=f1.readlines()
f.write(new_line)
cnt_lines=0
f.write(line3)
cnt_A=0
f.write(new_line)
cnt_B=0
f.close()
cnt_C=0
program2()
for lines in data:
cnt_lines+=1
[3] Write a program to read the
contents of both the files created if lines[0]=='A':
in the above programs and merge the cnt_A+=1
contents into “merge.txt”. Avoid if lines[0]=='B':
using the close() function to close cnt_B+=1
the files. if lines[0]=='C':
def program3(): cnt_C+=1
with open("MyFile.txt","r") as
print("Total Number of lines
f1:
data=f1.read() are:",cnt_lines)
with open("intro.txt","r") as print("Total Number of lines
f2: strating with A are:",cnt_A)
data1=f2.read() print("Total Number of lines
with open("merge.txt","w") as strating with B are:",cnt_B)
f3:
print("Total Number of lines
f3.write(data)
strating with C are:",cnt_C)
f3.write(data1)
program3() program5()

[6] Find the total occurrences of a


[4] Count the total number of upper specific word from a text file:
case, lower case, and digits used in def program6():
the text file “merge.txt”. cnt = 0
def program4(): word_search = input("Enter the
with open("merge.txt","r") as words to search:")
f1: with open("merge.txt","r") as
data=f1.read() f1:
cnt_ucase =0 for data in f1:
cnt_lcase=0 words = data.split()
for word in words: print(f.tell())
if (word == f.seek(4,0)
word_search): print(f.read(5))
cnt+=1 f.seek(10,0)
print(word_search, "found ", print(f.tell())
cnt, " times from the file") print(f.seek(7,0))
program6() print(f.read(10))
[7] Read first n no. letters from a program9()
text file, read the first line, read
a specific line from a text file. [10] Append the contents in entered
def program7(): by the user in the text file:
cnt = 0 def program10():
n = int(input("Enter no. text = input("Enter text to
characters to read:")) append in the file:")
with open("merge.txt","r") as with open("merge.txt","a") as
f1: f1:
line1=f1.readline() f1.write(text)
print("The first line of program10()
file:",line1)
nchar=f1.read(n) [11] Read the contents of file in
print("First n no. of reverse order:
characters:", nchar) def program11():
nline=f1.readlines() for i in
print("Line n:",nline[n]) reversed(list(open("merge.txt","r"))
program7() ):
print(i.rstrip())
[8] Replace all spaces from text program11()
with – (dash). [12] Replace multiple spaces with
def program8(): single space in a text file.
cnt = 0 def program12():
n = int(input("Enter no. f1 = open("merge.txt","rt")
characters to read:")) f2 = open("merge1.txt","wt")
with open("merge.txt","r") as for line in f1:
f1: f2.write('
data = f1.read() '.join(line.split()))
data=data.replace(' ','-') f1.close()
with open("merge.txt","w") as f2.close()
f1: program12()
f1.write(data) Method 2:
program8() import re
def program12():
9] Write a program to know the f1 = open("merge.txt","rt")
cursor position and print the text f2 = open("merge3.txt","wt")
according to below-given for line in f1:
specifications: f2.write(re.sub('\s+','
1. Print the initial position ',line))
2. Move the cursor to 4th f1.close()
position f2.close()
3. Display next 5 characters program12()
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")

You might also like