-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathalternative_ending.py
36 lines (27 loc) · 1.13 KB
/
alternative_ending.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
# -*- coding: utf-8 -*-
"""
Tack an alternative ending onto a great work of literature.
"""
import os
# Jane Austen wrote in ASCII (though she didn't know it at the time),
# so we don't strictly need to specify UTF-8 as an encoding,
# but this is included in case we adapt the program to a non-ASCII text.
filename = 'austen-sense.txt'
encoding = 'utf-8'
filepath = os.path.join('data', filename)
alt_file_prefix = 'alternative_'
alt_filepath = os.path.join('data', alt_file_prefix + filename)
alt_ending = '\n\nBut then I woke up and it was all a dream!\n'
end_text = 'the end'
# strip() so that the end of the string is really the final characters,
# and does not include any trailing newlines.
text = open(filepath, encoding=encoding).read().strip()
# Remove any ending that might already be present in the file
# (as in the Austen file for example).
if text.lower().endswith(end_text):
text = text[:-len(end_text)]
with open(alt_filepath, mode='w', encoding=encoding) as f:
f.write(text + alt_ending)
# Check the end of the new file to test it has worked.
new_text = open(alt_filepath, encoding=encoding).read()
print(new_text[-500:])