Combining inplace filtering and the setting of encoding in the fileinput module

encoding, file-io, python, python-3.2

Solution

Starting python 3.10 `fileinput.input()` accepts an encoding parameter

Problem

I am attempting to use the `fileinput` module's inplace filtering feature to rewrite an input file in place. Needed to set encoding (both for read and write) to `latin-1` and attempted to pass `openhook=fileinput.hook_encoded('latin-1')` to `fileinput.input` but was thwarted by the error ``` ValueError: FileInput cannot use an opening hook in inplace mode ``` Upon closer inspection I see that the `fileinput` documentation clearly states this: You cannot use inplace and openhook together How can I get around this?

Original source

Related problems