try:
with open(file_path, 'r', encoding='cp1251') as file:
content = file.read()
print(content)
except FileNotFoundError:
print(f"Файл '{file_path}' не найден.")
except Exception as e:
print(f"Произошла ошибка при чтении файла: {e}")
import chardet
def detect_encoding(file_path):
with open(file_path, 'rb') as file:
raw_data = file.read()
result = chardet.detect(raw_data)
encoding = result['encoding']
return encoding
file_path = 'file.txt'
encoding = detect_encoding(file_path)
with open(file_path, 'r', encoding=encoding) as file:
content = file.read()
print(content)