1. To get an encoding parameter in Python 2:

If you only need to support Python 2.6 and 2.7 you can use io.open instead of open. io is the new io subsystem for Python 3, and it exists in Python 2,6 ans 2.7 as well. Please be aware that in Python 2.6 (as well as 3.0) it's implemented purely in python and very slow, so if you need speed in reading files, it's not a good option.

If you need speed, and you need to support Python 2.6 or earlier, you can use codecs.open instead. It also has an encoding parameter, and is quite similar to io.open except it handles line-endings differently.

2. To get a Python 3 open() style file handler which streams bytestrings:

open(filename, 'rb')

Note the 'b', meaning 'binary'.

Answer from Lennart Regebro on Stack Overflow
🌐
Python
docs.python.org › 3 › library › codecs.html
codecs — Codec registry and base classes
The mode argument may be any binary mode acceptable to the built-in open() function; the 'b' is automatically added. encoding specifies the encoding which is to be used for the file.
🌐
Learn By Example
learnbyexample.org › python-open-function
Python open() Function - Learn By Example
April 21, 2020 - # Read a file in 'UTF-8' encoding f = open('myfile.txt', encoding='UTF-8')
🌐
Python Morsels
pythonmorsels.com › unicode-character-encodings-in-python
Unicode character encodings - Python Morsels
May 2, 2022 - On my machine, the default character encoding is utf-8. But on Windows, the default character encoding is usually cp1252. Note: Since Python 3.6, all files are read and written by Python using utf-8 by default, even on Windows.
🌐
University of Pittsburgh
sites.pitt.edu › ~naraehan › python3 › reading_writing_methods.html
Python 3 Notes: Reading and Writing Methods
Python 3 Notes [ HOME | LING 1330/2330 ] File Reading and Writing Methods << Previous Note Next Note >> On this page: open(), file.read(), file.readlines(), file.write(), file.writelines(), with open() as f:. Before proceeding, make sure you understand the concepts of file path and CWD.
🌐
Honeybadger
honeybadger.io › blog › python-character-encoding
Python developer's guide to character encoding - Honeybadger Developer Blog
March 6, 2023 - This is because, when working with ... a file using the open() method, Python automatically treats it as a text file to convert the bytes in the text file to a string with the encoding you want....
Find elsewhere
🌐
Song Genius API
melaniewalsh.github.io › Intro-Cultural-Analytics › 02-Python › 07-Files-Character-Encoding.html
Files & Character Encoding — Introduction to Cultural Analytics & Python
If you want to read or write a ... To open a file, you can use Python’s built-in open() function. ... Inside the open() function parentheses, you insert the filepath to be opened in quotation marks. You should also insert a character encoding, which we will talk more about ...
🌐
Curiousefficiency
python-notes.curiousefficiency.org › en › latest › python3 › text_file_processing.html
Processing Text Files in Python 3 - Alyssa Coghlan's Python Notes
Example: f = tokenize.open(fname) uses PEP 263 encoding markers to detect the encoding of Python source files (defaulting to UTF-8 if no encoding marker is detected)
🌐
Stanford
web.stanford.edu › class › archive › cs › cs106a › cs106a.1204 › handouts › py-file.html
Python File Reading
The form open(filename, encoding='utf-8') can specify the encoding to use to interpret the text file as unicode. If reading a file crashes with a "UnicodeDecodeError", probably the reading code needs to specify an encoding as above. Try the 'utf-8' encoding first, as many files are encoded with it.
🌐
GitHub
github.com › ageitgey › face_recognition
GitHub - ageitgey/face_recognition: The world's simplest facial recognition api for Python and the command line · GitHub
import face_recognition picture_of_me = face_recognition.load_image_file("me.jpg") my_face_encoding = face_recognition.face_encodings(picture_of_me)[0] # my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face! unknown_picture = face_recognition.load_image_file("unknown.jpg") unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0] # Now we can see the two face encodings are of the same person with `compare_faces`! results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding) if results[0] == True: print("It's a picture of me!") else: print("It's not a picture of me!") See this example to try it out. All the examples are available here. ... Recognize faces in live video using your webcam - Simple / Slower Version (Requires OpenCV to be installed)
Author: ageitgey
🌐
Kroki
kroki.io
Kroki!
When using GET requests, your diagram must be encoded in the URL using a deflate + base64 algorithm. But don't worry, if you're not familiar with deflate or base64 (or if you don't want to use them), you can also send your diagram as plain text using POST requests (see below). Let's take an example with a GraphViz "Hello World": hello.dot ... cat hello.dot | python -c "import sys; import base64; import zlib; print(base64.urlsafe_b64encode(zlib.compress(sys.stdin.read().encode('utf-8'), 9)).decode('ascii'))"
🌐
Substack
gonzoml.substack.com › gonzo ml › why i keep coming back to universal transformers
Why I Keep Coming Back to Universal Transformers
May 3, 2026 - UTM-Jax is open source, and I would be genuinely happy to see anyone try it on tasks beyond Sudoku — maze navigation, ARC subsets, formal-logic benchmarks, the addition tasks from the Saunshi et al. looped-transformer paper.
🌐
Docs by LangChain
docs.langchain.com › integrations by component › text splitter integrations
Text splitter integrations - Docs by LangChain
3 days ago - from langchain_text_splitters import CharacterTextSplitter text_splitter = CharacterTextSplitter.from_tiktoken_encoder( encoding_name="cl100k_base", chunk_size=100, chunk_overlap=0 ) texts = text_splitter.split_text(document)
🌐
LabEx
labex.io › tutorials › python-how-to-use-python-utf8-encoding-451217
How to use Python UTF8 encoding | LabEx
LabEx recommends mastering encoding techniques for robust text processing in Python. Working with text files requires careful handling of character encodings to ensure data integrity and compatibility. ## Reading files with specific encoding with open('example.txt', 'r', encoding='utf-8') as file: content = file.read() print(content) ## Writing files with UTF-8 encoding with open('output.txt', 'w', encoding='utf-8') as file: file.write("Python: 编程的魔力")
🌐
GeeksforGeeks
geeksforgeeks.org › python › detect-encoding-of-a-text-file-with-python
Detect Encoding of a Text file with Python - GeeksforGeeks
April 8, 2026 - Python provides the chardet library, which can automatically detect a file’s encoding.
🌐
LabEx
labex.io › tutorials › python-how-to-read-python-files-with-encoding-434796
How to read Python files with encoding | LabEx
Character encoding represents how characters are mapped to specific binary sequences. The most common encodings include: Python 3 natively supports Unicode and provides robust encoding mechanisms:
🌐
Glyphs
forum.glyphsapp.com › scripting
Python’s default encoding for reading a file becomes ASCII in macOS Sonoma - Scripting - Glyphs Forum
November 10, 2023 - Just upgraded to macOS Sonoma, and my Python scripting started to fail because I haven’t specifying encoding="UTF-8" explicitly for functions like file.read() (we all know it’s supposed to default to “UTF-8” on macOS, right?): UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 0: ordinal not in range(128) Can reproduce with a simple line in the Macro panel: open("path to non-ASCII file").read() Platform: both the latest stable and cutting edge versions of Glyphs: 3.1.2 (3...
🌐
Protocol Buffers
protobuf.dev › programming-guides › proto3
Language Guide (proto 3) | Protocol Buffers Documentation
You should use the field numbers 1 through 15 for the most-frequently-set fields. Lower field number values take less space in the wire format. For example, field numbers in the range 1 through 15 take one byte to encode. Field numbers in the range 16 through 2047 take two bytes.
🌐
OBS
obsproject.com
Open Broadcaster Software | OBS
OBS (Open Broadcaster Software) is free and open source software for video recording and live streaming. Stream to Twitch, YouTube and many other providers or record your own videos with high quality H264 / AAC encoding.