|
4 | 4 | # Define file path with an "r"-string |
5 | 5 | filepath = r'07-files/soliloquy.txt' |
6 | 6 |
|
7 | | -new_lines = ["To sleep, perchance to Dream; aye, there's the rub,", |
8 | | - "For in that sleep of death, what dreams may come,", |
| 7 | +new_lines = ["To sleep, perchance to dream; ay, there's the rub;", |
| 8 | + "For in that sleep of death what dreams may come", |
9 | 9 | "When we have shuffled off this mortal coil,", |
10 | 10 | "Must give us pause."] |
11 | 11 |
|
|
18 | 18 |
|
19 | 19 | # The line count will be the position given by `enumerate()`, plus 1 |
20 | 20 | num_lines = position + 1 |
21 | | -print(f"File is {num_lines} lines long.") |
| 21 | +print(f"\nFile is {num_lines} lines long.\n") |
| 22 | + |
| 23 | +print("*" * 10) |
| 24 | +print("Hamlet, Act III, scene I") |
| 25 | +print("*" * 10 + '\n') |
22 | 26 |
|
23 | 27 | # Open in append mode ('a') |
24 | 28 | with open(filepath, mode='a') as writer: |
25 | 29 |
|
26 | | - # Write each line in the list to the file, and add a newline character; |
| 30 | + # Append each line in the list to the file, and add a newline character; |
27 | 31 | # without it, all the new lines would have been concatenated. |
28 | 32 | for line in new_lines: |
29 | 33 | writer.write(line + '\n') |
30 | 34 |
|
31 | 35 | # Print the newly added-to file, with line numbers. |
32 | 36 | with open(filepath, mode='r') as reader: |
33 | 37 | for line_num, line in enumerate(reader, start=1): |
34 | | - print(line_num, line) |
| 38 | + print(line_num, line, end='') |
35 | 39 |
|
36 | | -print(f"Wrote {len(new_lines)} extra lines.") |
| 40 | +print(f"\nWrote {len(new_lines)} extra lines.") |
0 commit comments