Skip to content Skip to sidebar Skip to footer

Python Open File Error

I am trying to open some file and I know there are some errors in the file with UTF-8 encoding, so what I will do in python3 is open(fileName, 'r', errors = 'ignore') but now I

Solution 1:

Python 2 does not support this using the built-in open function. Instead, you have to uses codecs.

importcodecsf= codecs.open(fileName, 'r', errors = 'ignore')

This works in Python 2 and 3 if you decide you need to switch your python version in the future.

Solution 2:

For UTF-8 encoded files I would suggest io module.

#!/usr/bin/python
# -*- coding: utf-8 -*-

import io

f=io.open('file.txt', 'r',  encoding='utf8')
s=f.read()
f.close()

Post a Comment for "Python Open File Error"