forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request geekcomputers#1877 from muzakkirsaifi123/file_handler
update the code for more understanding
- Loading branch information
Showing
1 changed file
with
46 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |