Skip to content

Commit 7da6e92

Browse files
committed
fix
1 parent af8e781 commit 7da6e92

File tree

392 files changed

+5607
-4546
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

392 files changed

+5607
-4546
lines changed

1 File handle/File handle binary/Deleting record in a binary file.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
def bdelete():
55
# Opening a file & loading it
6-
with open("studrec.dat","rb") as F:
6+
with open("studrec.dat", "rb") as F:
77
stud = pickle.load(F)
88
print(stud)
99

1010
# Deleting the Roll no. entered by user
1111
rno = int(input("Enter the Roll no. to be deleted: "))
12-
with open("studrec.dat","wb") as F:
12+
with open("studrec.dat", "wb") as F:
1313
rec = [i for i in stud if i[0] != rno]
1414
pickle.dump(rec, F)
1515

1 File handle/File handle binary/File handle binary read (record in non list form).py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33

44
def binary_read():
5-
with open("studrec.dat","rb") as b:
5+
with open("studrec.dat", "rb") as b:
66
stud = pickle.load(b)
77
print(stud)
88

99
# prints the whole record in nested list format
1010
print("contents of binary file")
1111

1212
for ch in stud:
13-
1413
print(ch) # prints one of the chosen rec in list
1514

1615
rno = ch[0]

1 File handle/File handle binary/Update a binary file2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55

66
def update():
7-
87
with open("studrec.dat", "rb+") as File:
98
value = pickle.load(File)
109
found = False
1110
roll = int(input("Enter the roll number of the record"))
12-
11+
1312
for i in value:
1413
if roll == i[0]:
1514
print(f"current name {i[1]}")

1 File handle/File handle binary/question 1 (elegible for remedial, top marks).py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
Write a function remcount( ) to count the number of students who need
77
remedial class (student who scored less than 40 percent)
88
9-
10-
"""
9+
10+
"""
1111
# also find no. of children who got top marks
1212

1313
import pickle
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""
2-
Class resposible for counting words for different files:
3-
- Reduce redundant code
4-
- Easier code management/debugging
5-
- Code readability
2+
Class resposible for counting words for different files:
3+
- Reduce redundant code
4+
- Easier code management/debugging
5+
- Code readability
66
"""
77

8-
class Counter:
98

10-
def __init__(self, text:str) -> None:
9+
class Counter:
10+
def __init__(self, text: str) -> None:
1111
self.text = text
1212

1313
# Define the initial count of the lower and upper case.
@@ -16,20 +16,19 @@ def __init__(self, text:str) -> None:
1616
self.count()
1717

1818
def count(self) -> None:
19-
2019
for char in self.text:
2120
if char.lower():
2221
self.count_lower += 1
2322
elif char.upper():
2423
self.count_upper += 1
2524

2625
return (self.count_lower, self.count_upper)
27-
26+
2827
def get_total_lower(self) -> int:
2928
return self.count_lower
3029

3130
def get_total_upper(self) -> int:
3231
return self.count_upper
3332

3433
def get_total(self) -> int:
35-
return self.count_lower + self.count_upper
34+
return self.count_lower + self.count_upper
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,38 @@
1-
21
import os
32
import time
4-
file_name= input("Enter the file name to create:- ")
3+
4+
file_name = input("Enter the file name to create:- ")
55

66
print(file_name)
77

8-
def write_to_file(file_name):
98

9+
def write_to_file(file_name):
1010
if os.path.exists(file_name):
1111
print(f"Error: {file_name} already exists.")
1212
return
1313

1414
with open(file_name, "a") as F:
15-
1615
while True:
1716
text = input("enter any text to add in the file:- ")
18-
F.write( f"{text}\n" )
17+
F.write(f"{text}\n")
1918
choice = input("Do you want to enter more, y/n").lower()
2019
if choice == "n":
2120
break
22-
23-
def longlines():
2421

25-
with open(file_name, encoding='utf-8') as F:
22+
23+
def longlines():
24+
with open(file_name, encoding="utf-8") as F:
2625
lines = F.readlines()
27-
lines_less_than_50 = list( filter(lambda line: len(line) < 50, lines ) )
26+
lines_less_than_50 = list(filter(lambda line: len(line) < 50, lines))
2827

2928
if not lines_less_than_50:
3029
print("There is no line which is less than 50")
3130
else:
3231
for i in lines_less_than_50:
3332
print(i, end="\t")
3433

34+
3535
if __name__ == "__main__":
3636
write_to_file(file_name)
3737
time.sleep(1)
38-
longlines()
38+
longlines()

1 File handle/File handle text/input,output and error streams.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44
sys.stdout.write("Enter the name of the file")
55
file = sys.stdin.readline()
66

7-
with open(file.strip(), ) as F:
8-
7+
with open(
8+
file.strip(),
9+
) as F:
910
while True:
1011
ch = F.readlines()
11-
for (i) in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
12+
for i in ch: # ch is the whole file,for i in ch gives lines, for j in i gives letters,for j in i.split gives words
1213
print(i, end="")
1314
else:
1415
sys.stderr.write("End of file reached")
1516
break
16-
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,32 @@
1-
""" Write a method/function DISPLAYWORDS() in python to read lines
1+
"""Write a method/function DISPLAYWORDS() in python to read lines
22
from a text file STORY.TXT,
33
using read function
4-
and display those words, which are less than 4 characters. """
4+
and display those words, which are less than 4 characters."""
55

6+
print("Hey!! You can print the word which are less then 4 characters")
67

7-
print("Hey!! You can print the word which are less then 4 characters")
88

99
def display_words(file_path):
10-
1110
try:
1211
with open(file_path) as F:
1312
words = F.read().split()
14-
words_less_than_40 = list( filter(lambda word: len(word) < 4, words) )
13+
words_less_than_40 = list(filter(lambda word: len(word) < 4, words))
1514

1615
for word in words_less_than_40:
1716
print(word)
18-
19-
return "The total number of the word's count which has less than 4 characters", (len(words_less_than_40))
20-
17+
18+
return (
19+
"The total number of the word's count which has less than 4 characters",
20+
(len(words_less_than_40)),
21+
)
22+
2123
except FileNotFoundError:
2224
print("File not found")
2325

26+
2427
print("Just need to pass the path of your file..")
2528

2629
file_path = input("Please, Enter file path: ")
2730

2831
if __name__ == "__main__":
29-
3032
print(display_words(file_path))
31-
32-
33-
34-
35-

1 File handle/File handle text/question 5.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,29 @@
55
import os
66
from counter import Counter
77

8-
print("You will see the count of lowercase, uppercase and total count of alphabets in provided file..")
8+
print(
9+
"You will see the count of lowercase, uppercase and total count of alphabets in provided file.."
10+
)
911

1012

1113
file_path = input("Please, Enter file path: ")
1214

1315
if os.path.exists(file_path):
14-
print('The file exists and this is the path:\n',file_path)
16+
print("The file exists and this is the path:\n", file_path)
1517

1618

1719
def lowercase(file_path):
1820
try:
19-
2021
with open(file_path) as F:
2122
word_counter = Counter(F.read())
22-
23-
print(f"The total number of lower case letters are {word_counter.get_total_lower()}")
23+
24+
print(
25+
f"The total number of lower case letters are {word_counter.get_total_lower()}"
26+
)
2427
time.sleep(0.5)
25-
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")
28+
print(
29+
f"The total number of upper case letters are {word_counter.get_total_upper()}"
30+
)
2631
time.sleep(0.5)
2732
print(f"The total number of letters are {word_counter.get_total()}")
2833
time.sleep(0.5)
@@ -31,8 +36,5 @@ def lowercase(file_path):
3136
print("File is not exist.. Please check AGAIN")
3237

3338

34-
35-
3639
if __name__ == "__main__":
37-
3840
lowercase(file_path)

1 File handle/File handle text/question 6.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33

44
from counter import Counter
55

6-
def lowercase():
76

7+
def lowercase():
88
with open("happy.txt") as F:
99
word_counter = Counter(F.read())
10-
11-
print(f"The total number of lower case letters are {word_counter.get_total_lower()}")
12-
print(f"The total number of upper case letters are {word_counter.get_total_upper()}")
10+
11+
print(
12+
f"The total number of lower case letters are {word_counter.get_total_lower()}"
13+
)
14+
print(
15+
f"The total number of upper case letters are {word_counter.get_total_upper()}"
16+
)
1317
print(f"The total number of letters are {word_counter.get_total()}")
1418

19+
1520
if __name__ == "__main__":
1621
lowercase()

0 commit comments

Comments
 (0)