From 29717911363c484c65716b94463c638462928f7b Mon Sep 17 00:00:00 2001 From: Steve Schmerler Date: Tue, 28 Nov 2023 13:32:14 +0100 Subject: [PATCH] ENH: handle unicode enc error in file_write() One some (HPC) systems, we occasionally see random errors of the form UnicodeEncodeError: 'ascii' codec can't encode characters in position 4475-4476 when calling file_write() caused by run(..., capture_logs="file"). This may be caused by oldish file systems that can't handle unicode files. --- src/psweep/psweep.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/psweep/psweep.py b/src/psweep/psweep.py index baf9977..b8781a7 100644 --- a/src/psweep/psweep.py +++ b/src/psweep/psweep.py @@ -124,7 +124,10 @@ def flatten(seq): def file_write(fn: str, txt: str, mode="w"): makedirs(os.path.dirname(fn)) with open(fn, mode=mode) as fd: - fd.write(txt) + try: + fd.write(txt) + except UnicodeEncodeError: + fd.write(txt.encode("ascii", errors="xmlcharrefreplace").decode()) def file_read(fn: str):