Skip to content

UnicodeEncodeError on Wiindows #1

@YedTheEmo

Description

@YedTheEmo

UnicodeEncodeError on Windows (works fine on Linux/WSL)

Issue

When running codesplain.py on Windows, the following error occurs:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 75-77: character maps to <undefined>

This happens when writing out the structure file (or any output file with non-ASCII characters), typically under _generate_structure().


Stack Trace (Truncated)

File "codesplain.py", line 250, in _generate_structure
    f.write(structure)
...
UnicodeEncodeError: 'charmap' codec can't encode characters

Environment

  • OS: Windows 10/11 (PowerShell)
  • Python: 3.7+ (Anaconda or system install)
  • Terminal encoding: likely CP1252 (Windows default)

Workaround

Running the same command inside WSL (Ubuntu) or on Linux/macOS works fine:

wsl python3 codesplain.py /mnt/c/Users/xenomorph/emotune

Suggested Fix

Force UTF-8 encoding when writing files in the script to ensure cross-platform compatibility.

In all open(..., 'w') calls, replace with:

open(file_path, 'w', encoding='utf-8')

Or explicitly set the default encoding at the top of the script (not recommended long-term):

import sys
sys.stdout.reconfigure(encoding='utf-8')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions