#!/usr/bin/env python


import json
from os import scandir
from os.path import (
    join as path_join,
    basename as to_basename,
    splitext,
    exists
)

import ptt_core
l = ptt_core.l


_PREPROCESSED_DIR_PATH = 'preprocessed'


def preprocess_to_json_file(html_path):

    l.info('Preprocessing {} ...'.format(html_path))

    basename = to_basename(html_path)
    root, ext = splitext(basename)
    json_path = path_join(_PREPROCESSED_DIR_PATH, '{}.json'.format(root))

    if exists(json_path):
        l.info('Existed and skip {}'.format(json_path))
        return

    # read, parse,

    with open(html_path) as f:

        try:
            parsed_article_d = ptt_core.parse_article_page(f)
        except:
            # around 1.4% hit RuntimeError: the ending text of article may be
            # change. we just ignore them.
            l.info('Exception and skip {}'.format(json_path))
            return

    # and transform

    with ptt_core.mkdir_n_open(json_path, 'w') as f:
        json.dump(parsed_article_d, f)
    l.info('Wrote into {}'.format(json_path))


def preprocess_all(html_dir_path):

    for dir_entry in scandir(html_dir_path):

        path = dir_entry.path

        if 'index' in path:
            continue

        preprocess_to_json_file(path)


if __name__ == '__main__':

    preprocess_all('cache')

@@ -29,16 +29,41 @@
 rt json%0A
+from pathlib import Path%0A
 from os 
@@ -237,16 +237,21 @@
 _PATH = 
+Path(
 'preproc
@@ -255,17 +255,96 @@
 rocessed
-'
+/')%0A%0A%0Aif not _PREPROCESSED_DIR_PATH.exists():%0A    _PREPROCESSED_DIR_PATH.mkdir()
 %0A%0A%0Adef p
@@ -437,81 +437,8 @@
 ))%0A%0A
-    basename = to_basename(html_path)%0A    root, ext = splitext(basename)%0A
     
@@ -453,18 +453,8 @@
 h = 
-path_join(
 _PRE
@@ -471,17 +471,18 @@
 DIR_PATH
-,
+ /
  '%7B%7D.jso
@@ -495,13 +495,22 @@
 mat(
-root)
+html_path.stem
 )%0A%0A 
@@ -515,23 +515,16 @@
 %0A    if 
-exists(
 json_pat
@@ -524,16 +524,24 @@
 son_path
+.exists(
 ):%0A     
@@ -636,21 +636,16 @@
    with 
-open(
 html_pat
@@ -645,16 +645,22 @@
 tml_path
+.open(
 ) as f:%0A
@@ -988,41 +988,23 @@
 ith 
-ptt_core.mkdir_n_open(
 json
-_
+.
 path
-, 
+.open(
 'w')
@@ -1121,32 +1121,36 @@
 ll(html_dir_path
+_str
 ):%0A%0A    for dir_
@@ -1149,126 +1149,56 @@
 for 
-dir_entry in scandir(html_dir_path):%0A%0A        path = dir_entry.path%0A%0A        if 'index' in path:%0A            continue%0A
+path in Path(html_dir_path_str).glob('*index*'):
 %0A   

Use pathlib in the json