Article Directory
1. Preparation
The file name is "info.txt" and the
content of the file is as follows:
Welcome to learn Python language
Chapter 1: Python past and present
Chapter 2: Python language basics
Chapter 3: Python advanced advanced
2. Code
text_file = open('info.txt', 'rt', encoding='utf-8')
for line in text_file.readlines():
print(line, end='')
text_file.close()
or
text_file = open('info.txt', 'rt', encoding='utf-8')
content = text_file.read()
print(content)
text_file.close()
or
with open('info.txt', 'rt', encoding='utf-8') as text_file:
content = text_file.read()
print(content)
or
with open('info.txt', 'rt', encoding='utf-8') as text_file:
for line in text_file.readlines():
print(line, end='')
3. Results
4. Description
In my environment, if there is no parameter, "encoding=utf-8", an error will be reported.
The error content is: UnicodeDecodeError:'gbk' codec can't decode byte 0xad in position 20: illegal multibyte sequence