{
  "id": 18692345,
  "title": "My Python script read a file fine on Linux and crashed on Windows. The bug was open() without encoding",
  "url": "https://urgent.news/2026/11/07/my-python-script-read-a-file-fine-on-linux-and-crashed-on-windows-the",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-11-07T10:42:21.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/renev3408/my-python-script-read-a-file-fine-on-linux-and-crashed-on-windows-the-bug-was-open-without-3jgl"
  },
  "original_language": "en",
  "account": "A Python script that worked perfectly fine on a Linux server started crashing with a UnicodeDecodeError when run on a Windows laptop, despite having the same file and Python version. The issue stemmed from the open() function not specifying an encoding, causing Python to default to the system's preferred encoding which varied between Linux and Windows. On Linux, this was UTF-8, but on Windows, it was often cp1252 or another legacy code page. The fix was straightforward: always specify the encoding explicitly, such as encoding = \"utf-8\", when opening text files. This could be done using context managers with open(), or with pathlib's read_text() and write_text() methods. Python 3.10 introduced an EncodingWarning that would alert developers each time an open() was used without an explicit encoding. Running Python with the -X warn_default_encoding flag would also output these warnings. For situations where the code cannot be edited, setting PYTHONUTF8=1 in the environment would force UTF-8 encoding. While a handy stopgap, this should not replace the explicit encoding specification. Developers should also check their code for any open() or read_text() calls without an encoding, add encoding = \"utf-8\" as a habit, and use the -X warn_default_encoding flag during testing to catch any overlooked instances. This simple change could prevent many \"works on my machine\" bugs caused by OS defaults.",
  "summary": "The same script worked on my Linux server and raised UnicodeDecodeError on a Windows laptop. Same file, same code, same Python version. Here is why, and the one-argument fix. Reproduce it Create a UTF-8 file with a few non-ASCII characters: from pathlib import Path Path ( \" notes.txt \" ). write_bytes ( \" café ✓ naïve \" . encode ( \" utf-8 \" )) Now read it the lazy way: text = open ( \" notes.txt \"…",
  "key_points": [
    "Python script worked on Linux but crashed on Windows",
    "open() function lacked explicit encoding specification",
    "Adding encoding = utf-8 prevents UnicodeDecodeError"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}