constants: switch to pathlib

This makes the code a little simpler.

Change-Id: Id52a037fbf0330115844d9d95e7f4e4eb1fe16e9
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/docs/+/3369302
Tested-by: Mike Frysinger <vapier@chromium.org>
Auto-Submit: Mike Frysinger <vapier@chromium.org>
Reviewed-by: Lizzy Presland <zland@google.com>
Commit-Queue: Lizzy Presland <zland@google.com>
diff --git a/constants/errnos.py b/constants/errnos.py
index 048e7b2..73f5245 100755
--- a/constants/errnos.py
+++ b/constants/errnos.py
@@ -10,12 +10,16 @@
 
 import argparse
 import os
+from pathlib import Path
 import subprocess
 import sys
 
 import constants
 
 
+# The directory where all the code & markdown files live.
+TOPDIR = Path(__file__).resolve().parent
+
 # The C library header to find symbols.
 HEADER = 'errno.h'
 
@@ -118,10 +122,8 @@
     md_data = get_md_table(baseline, aliases)
 
     if opts.inplace:
-        md_file = os.path.join(os.path.dirname(os.path.abspath(__file__)),
-                               MARKDOWN)
-        with open(md_file) as fp:
-            old_data = fp.readlines()
+        md_file = TOPDIR / MARKDOWN
+        old_data = md_file.read_text().splitlines(keepends=True)
 
         i = None
         for i, line in enumerate(old_data):
@@ -133,7 +135,7 @@
             sys.exit(1)
 
         old_data = old_data[0:i + 2]
-        with open(md_file, 'w') as fp:
+        with md_file.open('w') as fp:
             fp.writelines(old_data)
             fp.write('\n'.join(md_data) + '\n')
     else: