File size: 484 Bytes
1600cd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"""Reading and writing plain text notes on disk.

One function opens a file and closes it by hand, which is the shape the context manager
pattern rewrites. The other already scopes its handle, and the pattern says so.
"""


def read_note(path):
    handle = open(path, "r", encoding="utf-8")
    body = handle.read()
    handle.close()
    return body


def write_note(path, body):
    with open(path, "w", encoding="utf-8") as handle:
        handle.write(body)
    return len(body)