Simply use the codecs module for writing the file:

import codecs
outputFile = codecs.open("textbase.tab", "w", "ISO-8859-1")

Of course, the strings you write have to be Unicode strings (type unicode), they won't be converted if they are plain str objects (which are basically just arrays of bytes). I guess you are reading the RTF file with the normal Python file object as well, so you might have to convert that to using codecs.open as well.

Answer from Torsten Marek on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › error decoding iso-8859-1 (latin1) text file. (python3.4)
r/learnpython on Reddit: Error decoding iso-8859-1 (Latin1) text file. (python3.4)
December 1, 2015 -

I have been using a small script to do some character replacement.

u = codecs.open(filename)

t = u.read().decode('iso-8859-1')

t = t.replace('º', 's') .

Still works on Ubuntu 12.04 flavor, python 3.2.

Now, if I try to run it on 14.04, python3.4, it quits at 't = u.read().decode('iso-8859-1')' with an error message:

File "/usr/lib/python3.4/codecs.py", line 319, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe3 in position 55: invalid continuation byte

Any ideas what is different between 3.2 and 3.4?

Edited a few letters.

🌐
Martin Thoma
martin-thoma.com › python-and-encodings
Python and Encodings · Martin Thoma
#!/usr/bin/env python # -*- coding: ... future.builtins import open # Specify the encoding while opening it with open("test-iso-8859-1.txt", encoding="ISO-8859-1") as f: content = f.read() content = content.encode("UTF-8", "replace") ...
Discussions

character encoding - Python: How do I force iso-8859-1 file output? - Stack Overflow
How do I force Latin-1 (which I guess means iso-8859-1?) file output in Python? Here's my code at the moment. It works, but trying to import the resulting output file into a Latin-1 MySQL table pr... More on stackoverflow.com
🌐 stackoverflow.com
python - How to read a "C source, ISO-8859 text" - Stack Overflow
Ah, indeed, but the data is UTF8 encoded, so the Latin-1 encoding I opened the file with didn't mind, nor did UTF8 fail. :-) Your browser must've adjusted the encoding after using it's own guesser. ... Sign up to request clarification or add additional context in comments. ... It took me 15 minutes of Googling to find this simple solution. Thanks! 2018-01-30T13:15:45.257Z+00:00 ... You change the codec in the open() command; the ISO-8859 ... More on stackoverflow.com
🌐 stackoverflow.com
Replace iso-8859-1 encoding symbols in python open module - Stack Overflow
Which python version are you using? The default encoding now is UTF8, not ascii. Not that it matters - 0xFF isn't valid in UTF8. Your file is definitely not ISO-8869-1 though. You need to find what codepage was used. Quite likely, it matches the country where the machine runs. Perhaps it's 8859-2 ... More on stackoverflow.com
🌐 stackoverflow.com
May 4, 2022
Encoding characters with ISO 8859-1 in Python - Stack Overflow
Your error message vaguely suggests that coding: iso-8859-1 is not actually true, and the file's encoding is actually something else (UTF-8 or UTF-16 would be my guess). The canonical must-read on character encoding in Python is http://nedbatchelder.com/text/unipain.html More on stackoverflow.com
🌐 stackoverflow.com
🌐
Mincong Huang
mincong.io › 2019 › 04 › 07 › understanding-iso-8859-1-and-utf-8
Understanding ISO-8859-1 / UTF-8 - Mincong Huang
April 7, 2019 - with open(path, 'r', encoding='ISO-8859-1') as f: for line in f: # ... If not given, it defaults to a platform dependent value. According to bultins.py: encoding is the name of the encoding used to decode or encode the file.
🌐
Python
bugs.python.org › issue20844
Issue 20844: SyntaxError: encoding problem: iso-8859-1 on Windows - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide · This issue has been migrated to GitHub: https://github.com/python/cpython/issues/65043
🌐
YouTube
youtube.com › scriptgpt
python open file encoding iso 8859 1 - YouTube
Download this code from https://codegive.com In this tutorial, we will explore how to open and manipulate files encoded with ISO 8859-1 (also known as Latin-...
Published: December 23, 2023
Views: 21
Find elsewhere
🌐
Coding
code.alshargi.us › home › load file, encoding iso-8859-1 , encoding utf-8 python code
Load file, encoding ISO-8859-1 , encoding utf-8 python code - Coding
November 23, 2022 - python, vb.net, ML code · by admin Posted on November 23, 2022November 23, 2022 · Load text from file to list: def load_txt_file(p): klist = [] with open(p) as fword: klist = fword.read().splitlines() return klist · Load text from file to list -encoding ISO-8859-1: def load_txt_file_ISO(p): all_data = [] with open(p, encoding = "ISO-8859-1") as fword: all_data = fword.read().splitlines() return all_data ·
🌐
Brainstorm
brainstorm.it › snippets › unicode-encoding-and-decoding
Unicode encoding and decoding - Brainstorm
$ iconv -f ISO-8859-1 -t UTF-8 source.txt > target.txt · to list all supported encodings: $ iconv -l · How to determine file encoding: $ file -i {filename} or (on osx): $ file -I {filename} Also check man page for enca: $ enca --help · If everything fails, install the following Python character ...
🌐
YouTube
youtube.com › codesolve
encoding iso 8859 1 in python - YouTube
Download this code from https://codegive.com Sure, I'd be happy to help you with a tutorial on encoding ISO 8859-1 in Python. ISO 8859-1, also known as Latin...
Published: December 28, 2023
Views: 47
Top answer
1 of 2
1

It's a mojibake case:

cmd

>NUL chcp 852
>dir_cp852.txt dir /C
type dir_cp852.txt | find /I "bytes free"
              28 Dir(s)  832 467 206 144 bytes free
>NUL chcp 1252
type dir_cp852.txt | find /I "bytes free"
              28 Dir(s)  832ÿ467ÿ206ÿ144 bytes free

Python

with open('dir_cp852.txt', 'r', encoding='iso-8859-1') as filename:
    file_content = filename.read()

print(file_content[-52:])
              28 Dir(s)  832ÿ467ÿ206ÿ144 bytes free

Solution:

with open('dir_cp852.txt', 'r', encoding='cp852') as filename:
    file_content = filename.read()

print(file_content[-52:])
              28 Dir(s)  832 467 206 144 bytes free

Note file_content[-52:] (in Python prompt):

'              28 Dir(s)  832\xa0467\xa0206\xa0144 bytes free\n'

shows character in mojibake: \xa0   (U+00A0, No-Break Space) with code 0xFF in Code page 852 (and more MS-DOS code pages).


Please note the /C switch in dir /C above (Display the thousand separator in file sizes).; I have overridden the default by (globally defined) set "DIRCMD=/-C".

The thousand separator in file sizes is defined in Control Panel\Clock and Region -> Region:
reg query "HKCU\Control Panel\International" /v sThousand

2 of 2
1

Use ISO-8859-2 instead:

filename = open(f'/opt/PATH/{shorter}', 'r', encoding='iso-8859-2')

You can also try cp1250 or windows-1250. The Windows-1250 codepage is slightly different from ISO-8859-2.

ascii refers to the 7-bit US-ASCII codepage. That codepage wouldn't be able to open your file either.

If you used dir in a cmd shell, use chcp 65001 to switch to UTF8 before executing the script. Or use Powershell Core instead.

Codepages

As I explained in the comments, ÿ isn't some kind of encoding symbol. Single-byte codepages like Latin1 (aka ISO-8859-1), Central/Eastern European codepages like ISO-8859-2, Cyrillic etc simply encode characters to single byte values. Encoding symbols appear only in Unicode and markup languages like HTML and XML.

The character you posted ÿ is encoded to 255 (0xFF) in ISO-8859-1. In the old IBM DOS codepages 437 or 852 that byte corresponds to a non-breaking space. In the other ISO-8859- codepages including the Eastern European ISO-8859-2 the value is used for a dot.

What happened

I suspect the file was created by redirecting the dir output on Windows' cmd shell. dir in Powershell doesn't have this footer. dir in cmd will use the user's (yours) locale to format dates and numbers. This means you could get different results if someone used a custom format. Linux shells also allow such localization and customization.

The cmd shell is non-Unicode though, so when you redirected the output the shell used the current codepage, which matches the user's locale, to encode the byte values. To use UTF8 you have to explicitly change the shell codepage with

chcp 65001

Better alternatives

Windows Terminal and Powershell use Unicode by default like Windows itself and don't have such problems. Redirecting even allows you to specify encodings, handle results as objects or tables and even output the data as CSV or HTML. cmd is essentially a legacy shell.

In Powershell/Powershell Core you could use :

Dir | Export-CSV C:\Users\username\Desktop\FileList.csv

To export the directory list to a properly formatted CSV file in UTF8

🌐
Tronche
tronche.com › wiki › Notes_on_python_unicode
Notes on python unicode - Tronche's wiki
November 27, 2015 - Many errors come from Python trying to implicitly encode. Here's an example (my terminal is set for UTF-8, LANG environment variable is en_GB.UTF-8, and thus python getdefaultencoding() is 'UTF-8'). input is ISO-8859-1, so I can type the é.
🌐
GitHub
gist.github.com › ansrivas › c4a9a045e26ba363c5b229a77f657df4
Convert iso-8859-1 to utf-8 in python · GitHub
# convert iso-8859-1 to unicode to utf-8, where `v` is the string in `iso-8859-1` format v.decode("iso-8859-1").encode("utf-8") ... If you have no way of finding out the correct encoding of the file, then try the following encodings, in this order: utf-8 iso-8859-1 (also known as latin-1) (This is the encoding of all census data and much other data produced by government entities.) utf-16```
🌐
SSOJet
ssojet.com › character-encoding-decoding › iso-8859-1-in-python
ISO 8859-1 in Python | Encoding Standards for Programming Languages
When working with files that use ... your text is handled correctly. To write data with ISO 8859-1, you'd use encoding='iso-8859-1' in your open() call....
🌐
Curiousefficiency
python-notes.curiousefficiency.org › en › latest › python3 › text_file_processing.html
Processing Text Files in Python 3 - Alyssa Coghlan's Python Notes
While the Windows cp1252 encoding ... UnicodeDecodeError. The latin-1 encoding in Python implements ISO_8859-1:1987 which maps all possible byte values to the first 256 Unicode code points, and thus ensures decoding errors will never occur regardless of the configured error ...
🌐
Compile7
compile7.org › character-encoding-decoding › how-to-handle-character-encoding-with-iso-8859-1-in-python
Encode and Decode with How to Handle Character Encoding with ISO 8859-1 in Python - Compile7
January 9, 2026 - When you need to save text data specifically in the ISO 8859-1 (Latin-1) encoding, you must first convert your Python strings into bytes using the correct encoding. The .encode('iso-8859-1') method is your go-to for this conversion.
🌐
KodeJungle
kodejungle.org › learning › character-encoding-decoding › beginner-guide-of-handle-character-encoding-iso-8859-1-using-python
Beginner Guide of Handle Character Encoding ISO 8859-1 using Python - KodeJungle
November 26, 2025 - When you're working with files ... encoding, leading to garbled text or errors. You achieve this by using the encoding parameter when opening files....
🌐
Python
docs.python.org › 3.4 › howto › unicode.html
Unicode HOWTO — Python 3.4.10 documentation
April 3, 2010 - If the code point is 128 or greater, the Unicode string can’t be represented in this encoding. (Python raises a UnicodeEncodeError exception in this case.) Latin-1, also known as ISO-8859-1, is a similar encoding.