Urgent.News

What's breaking now, across thousands of outlets.

Tech

My Python script read a file fine on Linux and crashed on Windows. The bug was open() without encoding

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 "…

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.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Secrets Management Explained: Stop Committing Credentials to GitHub

Secrets Management Explained: Stop Committing Credentials to GitHub Secrets management is the systematic process of storing, organizing, and controlling access to sensitive digital credentials such as…

  • Secrets management safeguards sensitive digital keys like passwords and API tokens.
  • Prevents credential leaks by keeping secrets out of version control systems.
  • Provides centralized access control and audit logging for security teams.

How to name a youtube series, then practise episode zero

how to name a youtube series: call it what the thing is called. your company, your project or your brand, in a word or two, and keep the episode number and the searchable words out of it.

  • Naming a YouTube series focuses on identifying the core subject.
  • The name should be concise and easily recognizable for returning viewers.
  • Episode titles should be tailored for search visibility.

More from Saturday 7 November →