对于Python2.7,请io.open()
在两个位置使用。
import io
import shutil
with 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)
上面的程序在我的电脑上运行没有错误。