Skip to content

Commit 7d0e718

Browse files
Merge pull request #1877 from muzakkirsaifi123/file_handler
update the code for more understanding
2 parents e6ce4e9 + 705025c commit 7d0e718

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed
Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,52 @@
11
"""Write a function in python to count the number of lowercase
22
alphabets present in a text file “happy.txt"""
33

4+
import time
5+
import os
6+
7+
print("You will see the count of lowercase, uppercase and total count of alphabets in provided file..")
8+
9+
10+
file_path = input("Please, Enter file path: ")
11+
12+
if os.path.exists(file_path):
13+
print('The file exists and this is the path:\n',file_path)
14+
15+
16+
def lowercase(file_path):
17+
try:
18+
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
36+
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)
43+
44+
except FileNotFoundError:
45+
print("File is not exist.. Please check AGAIN")
46+
47+
448

5-
def lowercase():
6-
with open("happy.txt") as F:
7-
count_lower = 0
8-
count_upper = 0
9-
value = F.read()
10-
for i in value:
11-
if i.islower():
12-
count_lower += 1
13-
elif i.isupper():
14-
count_upper += 1
15-
print("The total number of lower case letters are", count_lower)
16-
print("The total number of upper case letters are", count_upper)
17-
print("The total number of letters are", count_lower + count_upper)
1849

1950
if __name__ == "__main__":
20-
lowercase()
51+
52+
lowercase(file_path)

0 commit comments

Comments
 (0)