diff --git a/nanocode.py b/nanocode.py index bbe0bf57..047237b6 100755 --- a/nanocode.py +++ b/nanocode.py @@ -22,7 +22,8 @@ def read(args): - lines = open(args["path"]).readlines() + with open(args["path"], encoding="utf-8", errors="replace") as f: + lines = f.readlines() offset = args.get("offset", 0) limit = args.get("limit", len(lines)) selected = lines[offset : offset + limit] @@ -36,7 +37,8 @@ def write(args): def edit(args): - text = open(args["path"]).read() + with open(args["path"], encoding="utf-8") as f: + text = f.read() old, new = args["old"], args["new"] if old not in text: return "error: old_string not found" @@ -67,7 +69,7 @@ def grep(args): hits = [] for filepath in globlib.glob(args.get("path", ".") + "/**", recursive=True): try: - for line_num, line in enumerate(open(filepath), 1): + for line_num, line in enumerate(open(filepath, encoding="utf-8", errors="replace"), 1): if pattern.search(line): hits.append(f"{filepath}:{line_num}:{line.rstrip()}") except Exception: