Search code examples
pythonfile-ioencodingpython-3.2

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


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?


Solution

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