-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
43 lines (37 loc) · 1.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import urllib.request as url
def words(file):
return len(file.split())
def chars(file):
frequency = {}
for char in file:
try:
frequency[char.lower()] += 1
except:
frequency[char.lower()] = 1
return dict(sorted(frequency.items(), key=lambda item: item[1], reverse=True))
def main():
path = input("Please enter the path/link to your text: ")
file_contents = ""
try:
for result in url.urlopen(path):
file_contents += str(result)
except url.HTTPError:
print("Link not found. Please try again.")
main()
return
except ValueError:
try:
with open(path) as f:
file_contents = f.read()
except FileNotFoundError:
print("File not found. Please try again.")
main()
return
print(f"--- Begin report of {path} ---")
print(f"{words(file_contents)} words found in the document")
print(f"{len(file_contents)} characters found in the document")
print()
for i in chars(file_contents).items():
print(f"The '{i[0]}' character was found {i[1]} times")
print("--- End report ---")
main()