Note: Try to understand this programs and generate outputs with your pc
and make word file with outputs similar to your holiday assignment and
soft-copy will send on my number till Sunday 10:00pm
Read a text file and display the number of vowels/ consonants/ uppercase/
lowercase characters and other than character and digit in the file.
filein = open("[Link]",'r')
line = [Link]()
count_vow = 0
count_con = 0
count_low = 0
count_up = 0
count_digit = 0
count_other = 0
print(line)
for ch in line:
if [Link]():
count_up +=1
if [Link]():
count_low += 1
if ch in 'aeiouAEIOU':
count_vow += 1
if [Link]():
count_con += 1
if [Link]():
count_digit += 1
if not [Link]() and ch !=' ' and ch !='\n':
count_other += 1
print("Digits",count_digit)
print("Vowels: ",count_vow)
print("Consonants: ",count_con-count_vow)
print("Upper Case: ",count_up)
print("Lower Case: ",count_low)
print("other than letters and digit: ",count_other)
[Link]()
Write a Python code to find the size of the file in bytes, the number of lines,
number of words and no. of character.
import os
lines = 0
words = 0
letters = 0
filesize = 0
for line in open("[Link]"):
lines += 1
letters += len(line)
# get the size of file
filesize = [Link]("[Link]")
# A flag that signals the location outside the word.
pos = 'out'
for letter in line:
if letter != ' ' and pos == 'out':
words += 1
pos = 'in'
elif letter == ' ':
pos = 'out'
print("Size of File is",filesize,'bytes')
print("Lines:", lines)
print("Words:", words)
print("Letters:", letters)
Write a program that accepts a filename of a text file and reports the file's
longest line.
def get_longest_line(filename):
large_line = ''
large_line_len = 0
with open(filename, 'r') as f:
for line in f:
if len(line) > large_line_len:
large_line_len = len(line)
large_line = line
return large_line
filename = input('Enter text file Name: ')
print (get_longest_line(filename+".txt"))
Create a binary file with the name and roll number. Search for a given roll
number and display the name, if not found display appropriate message.
import pickle
def Writerecord(sroll,sname):
with open ('[Link]','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname}
[Link](srecord,Myfile)
def Readrecord():
with open ('[Link]','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print()
while True:
try:
rec=[Link](Myfile)
print(' ',rec['SROLL'],'\t ' ,rec['SNAME'])
except EOFError:
break
def Input():
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
Writerecord(sroll,sname)
def SearchRecord(roll):
with open ('[Link]','rb') as Myfile:
while True:
try:
rec=[Link](Myfile)
if rec['SROLL']==roll:
print("Roll NO:",rec['SROLL'])
print("Name:",rec['SNAME'])
except EOFError:
print("Record not find..............")
print("Try Again..............")
break
def main():
while True:
print('\nYour Choices are: ')
print('[Link] Records')
print('[Link] Records')
print('[Link] Records (By Roll No)')
print('[Link] (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
r=int(input("Enter a Rollno to be Search: "))
SearchRecord(r)
else:
break
main()
Create a binary file with roll number, name and marks. Input a roll number
and update details.
def Writerecord(sroll,sname,sperc,sremark):
with open ('[Link]','ab') as Myfile:
srecord={"SROLL":sroll,"SNAME":sname,"SPERC":sperc,
"SREMARKS":sremark}
[Link](srecord,Myfile)
def Readrecord():
with open ('[Link]','rb') as Myfile:
print("\n-------DISPALY STUDENTS DETAILS--------")
print("\nRoll No.",' ','Name','\t',end='')
print('Percetage',' ','Remarks')
while True:
try:
rec=[Link](Myfile)
print(' ',rec['SROLL'],'\t ' ,rec['SNAME'],'\t ',end='')
print(rec['SPERC'],'\t ',rec['SREMARKS'])
except EOFError:
break
def Input():
n=int(input("How many records you want to create :"))
for ctr in range(n):
sroll=int(input("Enter Roll No: "))
sname=input("Enter Name: ")
sperc=float(input("Enter Percentage: "))
sremark=input("Enter Remark: ")
Writerecord(sroll,sname,sperc,sremark)
def Modify(roll):
with open ('[Link]','rb') as Myfile:
newRecord=[]
while True:
try:
rec=[Link](Myfile)
[Link](rec)
except EOFError:
break
found=1
for i in range(len(newRecord)):
if newRecord[i]['SROLL']==roll:
name=input("Enter Name: ")
perc=float(input("Enter Percentage: "))
remark=input("Enter Remark: ")
newRecord[i]['SNAME']=name
newRecord[i]['SPERC']=perc
newRecord[i]['SREMARKS']=remark
found =1
else:
found=0
if found==0:
print("Record not found")
with open ('[Link]','wb') as Myfile:
for j in newRecord:
[Link](j,Myfile)
def main():
while True:
print('\nYour Choices are: ')
print('[Link] Records')
print('[Link] Records')
print('[Link] Records')
print('[Link] (Enter 0 to exit)')
ch=int(input('Enter Your Choice: '))
if ch==1:
Input()
elif ch==2:
Readrecord()
elif ch==3:
r =int(input("Enter a Rollno to be update: "))
Modify(r)
else:
break
main()
Remove all the lines that contain the character `a' in a file and write it to
another file
f1 = open("[Link]")
f2 = open("[Link]","w")
for line in f1:
if 'a' not in line:
[Link](line)
print('## File Copied Successfully! ##')
[Link]()
[Link]()
f2 = open("[Link]","r")
print([Link]())