-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHomeWork 8
More file actions
30 lines (24 loc) · 1.46 KB
/
HomeWork 8
File metadata and controls
30 lines (24 loc) · 1.46 KB
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
# Напишите функцию read_last(lines, file), которая будет открывать определенный файл file
# и выводить на печать построчно последние строки в количестве lines
# (на всякий случай проверим, что задано положительное целое число).
# Протестируем функцию на файле «article.txt» со следующим содержимым:
def read_last(lines, file):
if lines > 0:
with open(file, encoding='utf-8') as text:
file_lines = text.readlines()[-lines:]
for line in file_lines:
print(line.strip())
else:
print('Количество строк может быть только целым положительным')
read_last(4, 'article.txt')
# Требуется реализовать функцию longest_words(file), которая выводит слово,
# имеющее максимальную длину (или список слов, если таковых несколько).
def longest_words(file):
with open(file, encoding='utf 8') as text:
words = text.read().split()
max_length = len(max(words, key=len))
sought_words = [word for word in words if len(word) == max_length]
if len(sought_words) == 1:
return sought_words[0]
return sought_words
print(longest_words('article.txt'))