Skip to content

Commit

Permalink
Merge pull request geekcomputers#1877 from muzakkirsaifi123/file_handler
Browse files Browse the repository at this point in the history
update the code for more understanding
  • Loading branch information
geekcomputers authored Apr 1, 2023
2 parents e6ce4e9 + 705025c commit 7d0e718
Showing 1 changed file with 46 additions and 14 deletions.
60 changes: 46 additions & 14 deletions 1 File handle/File handle text/question 5.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,52 @@
"""Write a function in python to count the number of lowercase
alphabets present in a text file “happy.txt"""

import time
import os

print("You will see the count of lowercase, uppercase and total count of alphabets in provided file..")


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

if os.path.exists(file_path):
print('The file exists and this is the path:\n',file_path)


def lowercase(file_path):
try:

with open(file_path, 'r') as F:
# Define the initial count of the lower and upper case.
lowercase_count = 0
uppercase_count = 0

value = F.read()

for i in value:
if i.islower():
# It will increase the count.
lowercase_count += 1
elif i.isupper():
uppercase_count += 1



total_count = lowercase_count+uppercase_count

print("The total number of lower case letters are", lowercase_count)
time.sleep(1)
print("The total number of upper case letters are", uppercase_count)
time.sleep(1)
print("The total number of letters are", total_count)
time.sleep(1)

except FileNotFoundError:
print("File is not exist.. Please check AGAIN")



def lowercase():
with open("happy.txt") as F:
count_lower = 0
count_upper = 0
value = F.read()
for i in value:
if i.islower():
count_lower += 1
elif i.isupper():
count_upper += 1
print("The total number of lower case letters are", count_lower)
print("The total number of upper case letters are", count_upper)
print("The total number of letters are", count_lower + count_upper)

if __name__ == "__main__":
lowercase()

lowercase(file_path)

0 comments on commit 7d0e718

Please sign in to comment.