Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use an explicit text encoding at every file boundary: encoding='utf-8' for files defined as UTF-8, encoding='locale' only when the file contract deliberately follows the operating system locale, and binary mode when you are handling bytes. Python 3.15 enables UTF-8 mode by default when no encoding is supplied, but explicit arguments keep the format visible and preserve behavior when code runs on older Python versions.

The reliable default

For a known UTF-8 text file, open it in text mode with an explicit encoding and let a context manager close it:

from pathlib import Path

path = Path('notes.txt')

with path.open('r', encoding='utf-8', newline='') as f:
    text = f.read()

with path.open('w', encoding='utf-8', newline='') as f:
    f.write(text)

The built-in equivalent is open(path, 'r', encoding='utf-8') or open(path, 'w', encoding='utf-8'). The with statement closes the stream even if reading or writing raises an exception. For a simple whole-file operation, Path.read_text(encoding='utf-8') and Path.write_text(text, encoding='utf-8') are shorter. Use open() when you need newline control, append mode, streaming, or a carefully selected error policy.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What Python 3.15 changes—and what it does not

PEP 686 makes UTF-8 mode the default in Python 3.15 when text I/O is opened without an explicit encoding. The 3.15 I/O documentation and release notes still recommend passing an encoding for compatibility between Python versions. A file opened without encoding= is therefore less risky on 3.15 than on older releases, but it leaves the file format implicit and can change behavior when the same program is backported, embedded, or run with UTF-8 mode disabled.

#1 Best Overall
Sale
Compressed Air Duster,130000RPM Super Power Cordless Air Duster,Rechargeable Brushless Blower,3 Gear Adjustable Blower with LED Light,Dust Remover for Computer/House/Outdoor/Car
  • Powerful Turbo Fan:CAGIWIRU Electric Air Duster have reached speed up to 130000RPM,Can efficient remove dust and debris.With LED light and Three Adjustable Speed to meet with different cleaning tasks.
  • With Attached USB C Fasting Charging cable.Charing 3 hours,Enjoy up to 240 minutes of use on the lowest setting, For third speed can use for 25mins.with four charging options to meet with your needs.
  • Wide Application: attached 5 different nozzles,making it suitable for a variety of scenes, such as car,pet, pc, keyboards, and other electronic devices. It also serves for office and home clean duster.
  • Handy Design & Portable:Cordless air duster electric nozzle is Light weight and small size, design and comfortable to hold and space saving, convenient to carry around. You can put it in your room or inside the car without taking up space.
  • Economical &Reusable: Built durable materials with a long-lasting battery,our CAGIWIRU air duster it's a sustainable alternative to disposable air cans.

Python 3.15 does not infer the correct encoding for every file. UTF-8 is appropriate only when the producer and consumer agree that the bytes represent UTF-8. A legacy Windows code page, ISO-8859-1 export, or application-specific codec still requires its own explicit name. UTF-8 mode also does not turn binary data into text; bytes and text remain different interfaces.

Choose the right file interface

Situation Mode and arguments Why
Known UTF-8 text 'r', 'w', or 'a' with encoding='utf-8' Declares the format and gives consistent decoding or encoding across platforms.
Locale-defined text Text mode with encoding='locale' Requests the current locale encoding intentionally instead of relying on an undocumented default.
Raw bytes, archives, images, or protocol payloads 'rb' or 'wb' Preserves bytes; decode or encode only at the protocol boundary.
Very large text Text mode with explicit encoding, then iterate over the stream or read bounded chunks Avoids allocating the entire file as one string.
File must be extended 'a' with the same explicit encoding Writes at the end without truncating existing content.
Simultaneous reading and writing 'a+' or another combined mode, with an explicit encoding Combines operations, but requires deliberate cursor management.
Consumer requires a UTF-8 BOM encoding='utf-8-sig' Consumes a leading BOM when reading and writes one when creating output.

Read a UTF-8 file

Read the entire file

Whole-file reading is appropriate when the file comfortably fits in memory and your code needs one complete string:

from pathlib import Path

text = Path('notes.txt').read_text(encoding='utf-8')

With open(), the equivalent is:

with open('notes.txt', 'r', encoding='utf-8') as f:
    text = f.read()

Both forms decode the file’s bytes as UTF-8. With the default errors='strict', malformed byte sequences raise UnicodeDecodeError instead of silently changing the content.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Read line by line

Iteration keeps memory bounded and is usually the right shape for logs, exports, and other large files:

with open('events.log', 'r', encoding='utf-8') as f:
    for line_number, line in enumerate(f, start=1):
        process(line_number, line.rstrip('n'))

The stream decodes bytes as they are read. Do not call read() merely because it is convenient when the input can be many gigabytes; process each line or a bounded chunk instead.

Read controlled chunks

with open('archive.txt', 'r', encoding='utf-8') as f:
    while chunk := f.read(1024 * 1024):
        process_chunk(chunk)

Chunk boundaries are character-aware at the text-stream layer, so callers receive strings rather than arbitrary byte fragments. If a protocol requires exact byte boundaries, use binary mode and decode according to that protocol instead.

Rank #2
EVEO® Screen Cleaner Spray Kit (Streak-Free) TV Screen Cleaner for Smart TV, Lcd, LEd, Oled | Computer Screen Cleaner Spray and Wipe Microfiber Cloth - TV, Monitor, Laptop, iPad, Car - 6.7oz 1 Pack
  • Screen Cleaner is Your Screen’s New Bestfriend! - EVEO is proud to present a screen cleaner that’s perfectly suited for your TV. Meet your new screen cleaner, computer screen cleaner, laptop cleaner, iPad cleaner, Macbook,tv cleaner or any other electronic device. Our screen cleaner spray and wipe along with the included plush microfiber cloth, easily removes dust, fingerprints & other smudges on the screen’s surface. Clean effectively for a streak-free screen
  • Gentle on your Screens - Our screen cleaner spray and wipes is compatible to be used even for the most sensitive LCD, LED, CRT, and OLED screens in proper use. Be at ease knowing that this product is compatible with all the major brands: LG, SONY, Samsung, Sharp, iPad, iPhone, Android screens, Kindle, PC monitors - we’ve got you covered! Can be used as LCD screen cleaner, tv cleaner, smart tv screen cleaner, and more
  • Premium Super Soft Plush Microfiber Cloth - Included in this efficient screen cleaning kit. This screen cleaner spray and wipe has our signature plush microfiber cloth specially designed to comply with screens and monitors and leave them streak-free. Has been shown to effectively absorbs stubborn stains, zero streaks, and no fiber shedding. Always use this together with our screen cleaner for the best results
  • Easy-to-Use Screen Cleaner Kit + microfiber cleaning cloth - The EVEO Screen Cleaning Kit will easily be your new favorite tool when it comes to taking care of the things you love most, quick and easy solution for a streak-free screen. Simply screw on the sprayer gun on the screen cleaner bottle. Spray the cleaning liquid onto the microfiber cloth, then wipe the screen until desired cleanliness & shine is achieved.
  • Quality Screen Care Kit Worth Purchasing - Enjoy a convenient Screen Cleaner anytime, anywhere you need it. Your satisfaction is our top priority. We stand behind the quality of our products and that’s why, you shouldn’t be worrying at all. We manufacture the Best Screen Cleaner. and can be used for laptop cleaning kit, laptop screen cleaner, tv screen cleaner for smart tv. MacBook screen cleaner, monitor cleaner or screen cleaner spray for electronic devices with microfiber cleaning cloth

Write, replace, and append safely

Create or replace a file

Mode 'w' creates the file if it does not exist and truncates an existing file before writing. Make that destructive behavior explicit:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from pathlib import Path

output = Path('report.txt')
with output.open('w', encoding='utf-8', newline='') as f:
    f.write('Résumén')
    f.write('Total: 42n')

If an exception occurs partway through, the target may contain a partial result. For critical files, write to a temporary path and replace the destination only after the complete output succeeds; the replacement strategy must also account for the operating system and any concurrent readers.

Append without changing the format

Mode 'a' positions writes at the end and does not truncate existing data:

with open('events.log', 'a', encoding='utf-8', newline='') as f:
    f.write('new eventn')

Use the same encoding used for the existing file. Appending UTF-8 text to a file created with another codec produces a mixed-format file that no single decoder can reliably interpret. Use 'a+' only when you truly need both operations; after writing, seek deliberately before reading because the stream position is not automatically where your next read expects.

Use Path.write_text() for simple output

from pathlib import Path

Path('summary.txt').write_text(summary, encoding='utf-8')

This convenience method is clear for one-shot replacement. Switch to open() when you need newline='' , append mode, incremental output, or a non-default error handler.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Newlines are a separate decision

Encoding controls how bytes become characters and how characters become bytes. The newline argument controls newline translation; changing one does not change the other.

Rank #3
Sale
BladeHawks RGB Gaming Mouse Pad Extra Large Extended 31.5x12 Inch-Black | LED mouse pad with 14 lighting modes, covers keyboard & mouse, anti-slip rubber base, waterproof surface, USB powered
  • 【Large size】:Measuring 800 x 300 mm/ 31. 5×12 inches, which is wider and taller than others. Large mouse pad can perfectly fit your keyboard and mouse of any size, and there is also ample space for peripherals such as phones, tablets, etc.
  • 【LED backlight mouse pad】:Blade Hawks RGB mouse pad provides 7 static modes and 7 dynamic modes will give you the options you are looking for. Power-off memory function remembers the last lighting mode you selected. The super glow fiber Chroma will give you a colorful gaming setup that you want.
  • 【Anti-slip rubber base】:Made from Natural rubber, this gaming mousepad will firmly grip your desktop, allowing you to enjoy the ultimate precision in the games you are most passionate about. It also comes with a texture that enhances this ability even further.
  • 【Smooth and waterproof surface】: Micro-textured cloth surface, for extremely precise mouse control and a smooth movement. When liquid splashes on the gaming mouse pad, it forms water droplets and slides down. Will not delay your work or gameplay.
  • 【Plug and play】: This mouse and keyboard pad is powered by USB, plug and play, no driver required. One button control light modes. Press one time to change the lighting mode, press twice to change the brightness, press and hold about 3 seconds turn ON/OFF.
Setting Use it when Effect
Omit newline (the documented default) Ordinary text should use universal newline behavior Python recognizes common input line endings and applies normal text-stream translation.
newline='' Using the csv module or a consumer that must see newline characters unchanged The text layer leaves newline translation to the consumer.
An explicit newline value A format requires a specific line-ending policy Documents that policy at the file boundary; choose it together with the format specification.

For CSV, open the file with newline='' and pass the resulting text stream to csv.reader or csv.writer:

import csv

with open('people.csv', 'r', encoding='utf-8', newline='') as f:
    for row in csv.reader(f):
        handle(row)

Choose an error policy deliberately

The default errors='strict' is the safest choice when corrupted or mislabelled input must be detected. It raises an exception at the first invalid sequence, preserving the fact that the file is not valid UTF-8.

Policy Result Appropriate use
'strict' Raises on an encoding error Data validation, configuration, source code, and any workflow where silent loss is unacceptable.
'replace' Substitutes replacement characters for undecodable or unencodable data Only when a readable approximation is preferable to failure and the lost original characters are acceptable.
'surrogateescape' Maps certain undecodable bytes to surrogate code points so they can be carried through a Python string and potentially written back Specialized byte-preserving workflows; document and test the receiving system because the resulting string is not ordinary Unicode text.
with open('incoming.txt', 'r', encoding='utf-8', errors='strict') as f:
    text = f.read()

Do not use 'replace' as a generic fix for a decoding error. The replacement character does not identify the original byte and cannot reconstruct it later.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Handle a UTF-8 BOM only when the format requires it

A UTF-8 BOM is the byte sequence EF BB BF at the beginning of a file. It is not needed for ordinary UTF-8, and some tools that expect plain UTF-8 expose it as an unexpected leading character.

When an external consumer explicitly requires a BOM, use the utf-8-sig codec:

from pathlib import Path

with Path('for-consumer.txt').open('w', encoding='utf-8-sig', newline='') as f:
    f.write('First linen')

with Path('for-consumer.txt').open('r', encoding='utf-8-sig', newline='') as f:
    text = f.read()

On decoding, utf-8-sig consumes a leading BOM. On encoding, it writes one. Use ordinary utf-8 when the specification says UTF-8 without a BOM; do not add utf-8-sig merely because a file came from Windows.

Rank #4
Sale
Vaydeer Wrist Rest for Keyboard and Mouse, Computer Ergonomic Wrist Support Pad, Soft Memory Foam Arm Cushion for Desk, Palm Hand Office Laptop Typing
  • 【Softer and More Comfortable】Vaydeer wrist rest has unique diamond pattern, which is the combination of softness and aesthetics. The materials of wrist rest are improved into higher quality memory foam and covered with silky smooth lycra. The computer wrist rest makes you as comfortable and cushiony as like rest your wrists on clouds.
  • 【Ergonomic Wrist Saver】The wrist rests for keyboard and mouse comes with a 17.32×3.15×0.83 inch keyboard wrist pad and a 5.94×3.15×0.83 inch mouse wrist support. Based on ergonomic design, the unique concave shape is the perfect fit for your wrist joints. The wrist rest pad fits most computer keyboards and laptops, improve hand and wrist posture, release your wrist and arm stress.
  • 【Non-Slip Rubber Bottom】Featuring an anti-skid silicone base on the bottom, this wrist keyboard support stays firmly in place on your desk, preventing the padding from sliding around, ensuring stable and consistent wrist support during extended computer sessions.
  • 【Better Experience & Pain Relief】Our keyboard arm rest is beneficial to alleviate the soreness caused by direct contact and friction between your arm and a hard desk surface, reducing the risk of wrist fatigue or carpal tunnel. The soft texture of memory foam can evenly distribute the pressure around your wrists and provide good support with just enough give.
  • 【Helpful in Multiple Scenarios】Whether you're working, studying, writing, typing, gaming, this keyboard and mouse rest combo is an essential accessory to add comfort and support to your hands and wrists. It’s also a great gift for men, women, family, friend, coworker, gamer, teacher, etc.

Keep binary data and text at a clear boundary

Text mode automatically decodes input bytes and encodes output strings. Binary mode performs no character conversion:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
with open('payload.bin', 'rb') as f:
    payload = f.read()

message = payload.decode('utf-8')

with open('payload-copy.bin', 'wb') as f:
    f.write(payload)

Decode only when the protocol says the payload is UTF-8. If you are copying, hashing, compressing, parsing a binary format, or preserving an unknown byte sequence, stay in 'rb'/'wb' mode. Calling decode('utf-8') on arbitrary binary data can fail or produce a misleading string; encoding that string back is not a substitute for preserving the original bytes.

Locale-defined files and cross-version behavior

Some applications intentionally use the operating system’s locale encoding because another program defines the file that way. In Python 3.15, request that contract explicitly with encoding='locale':

with open('legacy-export.txt', 'r', encoding='locale') as f:
    text = f.read()

This is different from saying the file is UTF-8. If the producer’s format is a named legacy codec, use that codec’s name instead of making the result depend on whichever locale happens to be active.

Python 3.15 allows PYTHONUTF8=0 and -X utf8=0 when a deployment must retain the older locale-dependent default. Code that relies on an implicit default can therefore still vary by launch configuration. Explicit encoding='utf-8', encoding='locale', or a named legacy codec makes the intended contract reviewable and keeps behavior stable when code is run under Python 3.11, 3.12, 3.13, 3.14, or a different 3.15 mode.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Migrate older code that omitted encoding

Use Python’s warning support to locate implicit-encoding calls before changing them:

Best Value
Sale
Under Desk Cable Management Tray No Drill, Metal Mesh Cable Management Under Desk with Clamp Mount, 2 Hole Cable Tray with Wire Management and Cord Organizer for Home Office Standing Desk
  • 【No Drill Mounted】Cable management tray can be assembled in as little as 3 minutes or less-simply. Side clips easily clampe the edge of desk, pass through the holes on the sides, and secure excess cables with baskets. Desk cable management supports inward or outward installation to meet your needs in different scenarios, and provides you with great convenience when collecting and organizing wires.
  • 【Rubber Pad Protects Desk】Wire organizer under desk built-in soft rubber pads on the clamp protect your desk from scratches, so you will not damage your furniture or office.
  • 【Sturdy and Beautiful】Cord management under desk is made of high-quality carbon steel and holds about 15 lbs. The wire basket has 2 holes on each side of the tray to enable your cables to pass freely. Cable baskets are suitable for desk thickness use from 0.4" to 2.4".
  • 【Space Saving & Keep Organized】Desk cable management tray holds all your power cords, outlet strips, USB hubs and computer transformers hidden from your feet, and the data cables will not enter the vacuum cleaner to keep your desk clean and tidy. Desk wire organizer is also suitable for use in the kitchen and outdoors.
  • 【Reduce Safety Risks】Cable tray under desk keeps all plugs away from your kids, no more worry about tripping. Using 2 holes wire tray saves extra space, helps you easily cabling and wire protection. The mesh design is not easy to accumulate dust, but avoids safety accidents caused by messy wires.
python -X warn_default_encoding your_program.py

You can enable the same warning through the PYTHONWARNDEFAULTENCODING environment variable. The warning is a discovery aid, not a decision about the correct codec. For each reported call:

  1. Identify the file contract. Check the producing application, format specification, or protocol documentation rather than guessing from the machine on which the warning appeared.
  2. Declare UTF-8 when the contract is UTF-8. Add encoding='utf-8' at the open call or use the corresponding Path method argument.
  3. Declare locale behavior when it is intentional. Use encoding='locale' instead of preserving an accidental default.
  4. Name a legacy codec when required. Use the documented codec, and add tests containing representative non-ASCII characters.
  5. Separate binary paths. Change byte-oriented operations to 'rb' or 'wb' and decode or encode at the boundary.
  6. Run the tests under multiple configurations. Include an older supported Python version and, where relevant, Python 3.15 with UTF-8 mode both enabled and disabled.

Troubleshoot common failures

UnicodeDecodeError while reading

  • Verify that the producer actually wrote UTF-8; a file extension does not establish an encoding.
  • Check whether the file has a BOM and whether the consumer expects it. Try utf-8-sig only when that is the documented format.
  • Inspect the byte offset named by the exception and identify the producing codec. Do not switch to errors='replace' unless data loss is acceptable.
  • If the input is a byte protocol rather than text, reopen it in binary mode and parse its framing before decoding fields.

Unexpected characters at the beginning

A visible marker such as ufeff usually means a BOM was decoded as ordinary UTF-8. Read with utf-8-sig for a known BOM-bearing format, or remove the BOM at the format boundary only after confirming that it is not meaningful data.

Newlines changed after a round trip

Review the newline argument independently of encoding. Use newline='' for CSV and other consumers that perform their own newline handling. For ordinary text, use the documented default unless the file format mandates a particular line ending.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Output was overwritten

Mode 'w' truncates before writing. Use 'a' for append-only logs, or write to a temporary file and replace the destination after a successful complete write when replacement must be atomic for your platform and workflow.

Code works on one computer but not another

Look for omitted encoding arguments, locale-dependent launches, and files produced by different tools. Add explicit arguments based on the format contract and run with -X warn_default_encoding to find remaining implicit cases.

Test the file boundary, not just the Python string

A test that checks only an in-memory value can miss an incorrect codec or newline policy. Include a round trip through the actual file interface:

from pathlib import Path

path = Path('roundtrip.txt')
original = 'café — 東京nline twon'
path.write_text(original, encoding='utf-8')
assert path.read_text(encoding='utf-8') == original

Useful test cases include accented Latin characters, non-Latin scripts, emoji, an empty file, a final line with and without a newline, malformed bytes that must fail under 'strict', and a BOM-bearing fixture when your integration requires one. For CSV, test the reader and writer with newline=''. For binary protocols, assert byte-for-byte equality before and after any operation that is supposed to preserve payloads.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A practical decision checklist

  • Is the file format documented as UTF-8? Use text mode with encoding='utf-8'.
  • Does another system define the file using the current locale? Use encoding='locale' explicitly.
  • Is the content bytes rather than text? Use 'rb'/'wb' and convert at a documented boundary.
  • Could the file be large? Iterate or process bounded chunks.
  • Does a consumer require a BOM? Use utf-8-sig; otherwise use ordinary UTF-8.
  • Does a consumer own newline handling, such as the CSV module? Pass newline=''.
  • Must bad input be detected? Keep errors='strict'.
  • Are you modernizing old code? Run -X warn_default_encoding, then choose an encoding from the file contract.

Python 3.15’s UTF-8 default removes one source of platform variation, but it does not replace an explicit format decision. Declaring the encoding, newline policy, error behavior, and binary-versus-text boundary at the point of I/O makes file handling predictable, reviewable, and portable.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.