-
Notifications
You must be signed in to change notification settings - Fork 0
/
readfiles.py
executable file
·65 lines (54 loc) · 1.29 KB
/
readfiles.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#! /usr/bin/env python3
text_file = 'test.txt'
def read_file(text_file):
"""
Function that reads a text file and returns the data from the text file
"""
try:
with open(text_file,'r') as handle:
data = handle.read()
return data
except FileNotFoundError:
return None
def read_words(text_file):
'''
Function that counts the number of times a word appears in a text file.
'''
with open(text_file,'r') as handle:
data = handle.read()
counter = 0
for word in data.split():
if word == "Python":
counter += 1
return counter
def read_lines(text_file):
'''
Function that counts the number of lines in a text file.
'''
with open(text_file,'r') as handle:
data = handle.readlines()
counter2 = 0
for line in data:
counter2 += 1
return counter2
def longest_word(text_file):
'''
Test to confirm the longest word in a text file
'''
with open('test.txt','r') as handle:
data = handle.read()
data = data.replace(',','')
data = data.replace('.','')
count = 0
for word in data.split():
if len(word) > count:
count = len(word)
# print(count)
for word in data.split():
if len(word) == count:
return word
if __name__ == '__main__':
read_file(text_file)
read_words(text_file)
read_lines(text_file)
longest_word(text_file)