Is 'encoding is an invalid keyword' error inevitable in python 2.x? Is 'encoding is an invalid keyword' error inevitable in python 2.x? python-3.x python-3.x

Is 'encoding is an invalid keyword' error inevitable in python 2.x?


For Python2.7, Use io.open() in both locations.

import ioimport shutilwith io.open('/etc/passwd', encoding='latin-1', errors='ignore') as source:    with io.open('/tmp/goof', mode='w', encoding='utf-8') as target:        shutil.copyfileobj(source, target)

The above program runs without errors on my PC.


This is how you can convert ansi to utf-8 in Python 2 (you just use normal file objects):

with open(file_path_ansi, "r") as source:    with open(file_path_utf8, "w") as target:        target.write(source.read().decode("latin1").encode("utf8"))


TypeError: 'encoding' is an invalid keyword argument for this function

open('textfile.txt', encoding='utf-16')

Use io, it will work in both 2.7 and 3.6 python version

import ioio.open('textfile.txt', encoding='utf-16')