File tree Expand file tree Collapse file tree 12 files changed +175
-188
lines changed
Expand file tree Collapse file tree 12 files changed +175
-188
lines changed Original file line number Diff line number Diff line change @@ -10,11 +10,7 @@ def bdelete():
1010 # Deleting the Roll no. entered by user
1111 rno = int (input ("Enter the Roll no. to be deleted: " ))
1212 with open ("studrec.dat" ) as F :
13- rec = []
14- for i in stud :
15- if i [0 ] == rno :
16- continue
17- rec .append (i )
13+ rec = [i for i in stud if i [0 ] != rno ]
1814 pickle .dump (rec , F )
1915
2016
Original file line number Diff line number Diff line change 44
55
66def update ():
7- F = open ("class.dat" , "rb+" )
8- S = pickle .load (F )
9- found = 0
10- rno = int (input ("enter the roll number you want to update" ))
11- for i in S :
12- if rno == i [0 ]:
13- print ("the currrent name is" , i [1 ])
14- i [1 ] = input ("enter the new name" )
15- found = 1
16- break
7+ with open ("class.dat" , "rb+" ) as F :
8+ S = pickle .load (F )
9+ found = False
10+ rno = int (input ("enter the roll number you want to update" ))
1711
18- if found == 0 :
19- print ("Record not found" )
12+ for i in S :
13+ if rno == i [0 ]:
14+ print (f"the currrent name is { i [1 ]} " )
15+ i [1 ] = input ("enter the new name" )
16+ found = True
17+ break
2018
21- else :
22- F .seek (0 )
23- pickle .dump (S , F )
19+ if found :
20+ print ("Record not found" )
2421
25- F .close ()
22+ else :
23+ F .seek (0 )
24+ pickle .dump (S , F )
2625
2726
2827update ()
2928
30- F = open ("class.dat" , "rb" )
31- val = pickle .load (F )
32- print (val )
33- F .close ()
29+ with open ("class.dat" , "rb" ) as F :
30+ print (pickle .load (F ))
Original file line number Diff line number Diff line change 1- # updating records in a bnary file
1+ # updating records in a binary file
22
33import pickle
44
55
66def update ():
7- File = open ("studrec.dat" , "rb+" )
8- value = pickle .load (File )
9- found = 0
10- roll = int (input ("Enter the roll number of the record" ))
11- for i in value :
12- if roll == i [0 ]:
13- print ("current name" , i [1 ])
14- print ("current marks" , i [2 ])
15- i [1 ] = input ("Enter the new name" )
16- i [2 ] = int (input ("Enter the new marks" ))
17- found = 1
18-
19- if found == 0 :
20- print ("Record not found" )
21-
22- else :
23- pickle .dump (value , File )
24- File .seek (0 )
25- newval = pickle .load (File )
26- print (newval )
27-
28- File .close ()
7+
8+ with open ("studrec.dat" , "rb+" ) as File :
9+ value = pickle .load (File )
10+ found = False
11+ roll = int (input ("Enter the roll number of the record" ))
12+
13+ for i in value :
14+ if roll == i [0 ]:
15+ print (f"current name { i [1 ]} " )
16+ print (f"current marks { i [2 ]} " )
17+ i [1 ] = input ("Enter the new name" )
18+ i [2 ] = int (input ("Enter the new marks" ))
19+ found = True
20+
21+ if not found :
22+ print ("Record not found" )
23+
24+ else :
25+ pickle .dump (value , File )
26+ File .seek (0 )
27+ print (pickle .load (File ))
2928
3029
3130update ()
Original file line number Diff line number Diff line change 1212
1313import pickle
1414
15- F = open ("class.dat" , "ab" )
1615list = [
1716 [1 , "Ramya" , 30 ],
1817 [2 , "vaishnavi" , 60 ],
2423 [8 , "sandhya" , 65 ],
2524]
2625
27-
28- pickle .dump (list , F )
29- F .close ()
26+ with open ( "class.dat" , "ab" ) as F :
27+ pickle .dump (list , F )
28+ F .close ()
3029
3130
3231def remcount ():
33- F = open ("class.dat" , "rb" )
34- val = pickle .load (F )
35- count = 0
32+ with open ("class.dat" , "rb" ) as F :
33+ val = pickle .load (F )
34+ count = 0
3635
37- for i in val :
38- if i [2 ] <= 40 :
39- print (i , "eligible for remedial" )
40- count += 1
41- print ("the total number of students are" , count )
42- F .close ()
36+ for i in val :
37+ if i [2 ] <= 40 :
38+ print (f"{ i } eligible for remedial" )
39+ count += 1
40+ print (f"the total number of students are { count } " )
4341
4442
4543remcount ()
4644
4745
4846def firstmark ():
49- F = open ("class.dat" , "rb" )
50- val = pickle .load (F )
51- main = []
52- count = 0
53-
54- for i in val :
55- data = i [2 ]
56- main .append (data )
47+ with open ("class.dat" , "rb" ) as F :
48+ val = pickle .load (F )
49+ count = 0
50+ main = [i [2 ] for i in val ]
5751
58- top = max (main )
59- print (top , "is the first mark" )
52+ top = max (main )
53+ print (top , "is the first mark" )
6054
61- F .seek (0 )
62- for i in val :
63- if top == i [2 ]:
64- print (i )
65- print ("congrats" )
66- count += 1
55+ F .seek (0 )
56+ for i in val :
57+ if top == i [2 ]:
58+ print (f"{ i } \n congrats" )
59+ count += 1
6760
68- print ("the total number of students who secured top marks are" , count )
69- F .close ()
61+ print ("the total number of students who secured top marks are" , count )
7062
7163
7264firstmark ()
7365
74- F = open ("class.dat" , "rb" )
75- val = pickle .load (F )
76- print (val )
77- F .close ()
66+ with open ("class.dat" , "rb" ) as F :
67+ val = pickle .load (F )
68+ print (val )
Original file line number Diff line number Diff line change 44
55
66def binary_search ():
7- F = open ("studrec.dat" , "rb" )
8- # your file path will be different
9- value = pickle .load (F )
10- search = 0
11- rno = int (input ("Enter the roll number of the student" ))
7+ with open ("studrec.dat" , "rb" ) as F :
8+ # your file path will be different
9+ search = 0
10+ rno = int (input ("Enter the roll number of the student" ))
1211
13- for i in value :
14- if i [0 ] == rno :
15- print ("Record found successfully" )
16- print (i )
17- search = 1
12+ for i in pickle .load (F ):
13+ if i [0 ] == rno :
14+ print (f"Record found successfully\n { i } " )
15+ search = 1
1816
19- if search == 0 :
20- print ("Sorry! record not found" )
21- F .close ()
17+ if search == 0 :
18+ print ("Sorry! record not found" )
2219
2320
2421binary_search ()
Original file line number Diff line number Diff line change 1+ """
2+ Class resposible for counting words for different files:
3+ - Reduce redundant code
4+ - Easier code management/debugging
5+ - Code readability
6+ """
7+
8+ class Counter :
9+
10+ def __init__ (self , text :str ) -> None :
11+ self .text = text
12+
13+ # Define the initial count of the lower and upper case.
14+ self .count_lower = 0
15+ self .count_upper = 0
16+ self .count ()
17+
18+ def count (self ) -> None :
19+
20+ for char in self .text :
21+ if char .lower ():
22+ self .count_lower += 1
23+ elif char .upper ():
24+ self .count_upper += 1
25+
26+ return (self .count_lower , self .count_upper )
27+
28+ def get_total_lower (self ) -> int :
29+ return self .count_lower
30+
31+ def get_total_upper (self ) -> int :
32+ return self .count_upper
33+
34+ def get_total (self ) -> int :
35+ return self .count_lower + self .count_upper
Original file line number Diff line number Diff line change @@ -9,28 +9,28 @@ def write_to_file(file_name):
99
1010 if os .path .exists (file_name ):
1111 print (f"Error: { file_name } already exists." )
12+ return
1213
13- else :
14- with open (file_name , "a" ) as F :
15- while True :
16- text = input ("enter any text to add in the file:- " )
17- F .write (
18- text + "\n "
19- ) # write function takes exactly 1 arguement so concatenation
20- choice = input ("Do you want to enter more, y/n" )
21- if choice == "n" :
22- break
23-
14+ with open (file_name , "a" ) as F :
15+
16+ while True :
17+ text = input ("enter any text to add in the file:- " )
18+ F .write ( f"{ text } \n " )
19+ choice = input ("Do you want to enter more, y/n" ).lower ()
20+ if choice == "n" :
21+ break
22+
2423def longlines ():
24+
2525 with open (file_name , encoding = 'utf-8' ) as F :
2626 lines = F .readlines ()
27+ lines_less_than_50 = list ( filter (lambda line : len (line ) < 50 , lines ) )
2728
28- for i in lines :
29- if len (i ) < 50 :
29+ if not lines_less_than_50 :
30+ print ("There is no line which is less than 50" )
31+ else :
32+ for i in lines_less_than_50 :
3033 print (i , end = "\t " )
31- else :
32- print ("There is no line which is less than 50 " )
33-
3434
3535if __name__ == "__main__" :
3636 write_to_file (file_name )
Original file line number Diff line number Diff line change 88
99def display_words (file_path ):
1010
11-
1211 try :
13- with open (file_path , 'r' ) as F :
14- lines = F .read ()
15- words = lines .split ()
16- count = 0
17- for word in words :
18- if (len (word ) < 4 ):
19- print (word )
20- count += 1
21- return "The total number of the word's count which has less than 4 characters" , (count )
12+ with open (file_path ) as F :
13+ words = F .read ().split ()
14+ words_less_than_40 = list ( filter (lambda word : len (word ) < 4 , words ) )
15+
16+ for word in words_less_than_40 :
17+ print (word )
18+
19+ return "The total number of the word's count which has less than 4 characters" , (len (words_less_than_40 ))
2220
2321 except FileNotFoundError :
2422 print ("File not found" )
Original file line number Diff line number Diff line change 11"""Write a function in python to count the number of lowercase
22alphabets present in a text file “happy.txt"""
33
4- import time
5- import os
4+ import time , os
5+ from counter import Counter
66
77print ("You will see the count of lowercase, uppercase and total count of alphabets in provided file.." )
88
1616def lowercase (file_path ):
1717 try :
1818
19- with open (file_path , 'r' ) as F :
20- # Define the initial count of the lower and upper case.
21- lowercase_count = 0
22- uppercase_count = 0
23-
24- value = F .read ()
25-
26- for i in value :
27- if i .islower ():
28- # It will increase the count.
29- lowercase_count += 1
30- elif i .isupper ():
31- uppercase_count += 1
32-
33-
34-
35- total_count = lowercase_count + uppercase_count
19+ with open (file_path ) as F :
20+ word_counter = Counter (F .read ())
3621
37- print ("The total number of lower case letters are" , lowercase_count )
38- time .sleep (1 )
39- print ("The total number of upper case letters are" , uppercase_count )
40- time .sleep (1 )
41- print ("The total number of letters are" , total_count )
42- time .sleep (1 )
22+ print (f "The total number of lower case letters are { word_counter . get_total_lower () } " )
23+ time .sleep (0.5 )
24+ print (f "The total number of upper case letters are { word_counter . get_total_upper () } " )
25+ time .sleep (0.5 )
26+ print (f "The total number of letters are { word_counter . get_total () } " )
27+ time .sleep (0.5 )
4328
4429 except FileNotFoundError :
4530 print ("File is not exist.. Please check AGAIN" )
You can’t perform that action at this time.
0 commit comments