Skip to content

Commit 3c9ac75

Browse files
Merge pull request #2 from geekcomputers/master
Update in few files
2 parents 5177bbb + bef27e0 commit 3c9ac75

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

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

+25-11
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,33 @@
33
using read function
44
and display those words, which are less than 4 characters. """
55

6-
def display_words():
7-
with open("story.txt") as F:
8-
lines = F.read()
9-
words = lines.split()
10-
count = 0
11-
for word in words:
12-
if (len(word) < 4):
13-
print(word)
14-
count += 1
15-
return count
6+
7+
print("Hey!! You can print the word which are less then 4 characters")
8+
9+
def display_words(file_path):
10+
11+
12+
try:
13+
with open(file_path, 'r') as F:
14+
lines = F.read()
15+
words = lines.split()
16+
count = 0
17+
for word in words:
18+
if (len(word) < 4):
19+
print(word)
20+
count += 1
21+
return "The total number of the word's count which has less than 4 characters", (count)
22+
23+
except FileNotFoundError:
24+
print("File not found")
25+
26+
print("Just need to pass the path of your file..")
27+
28+
file_path = input("Please, Enter file path: ")
1629

1730
if __name__ == "__main__":
18-
print(display_words())
31+
32+
print(display_words(file_path))
1933

2034

2135

0 commit comments

Comments
 (0)