python 读取文件可以使用open()函数的 r 模式:
第一步:创建文件对象
第二步:读取文件
第三步:关闭文件
read():不带参数,读取整个文件
示例代码:
python
# 第一步:创建文件对象
file = open(file='a.txt', mode='r', encoding='utf-8')
# 第二步:读取文件
print(file.read())
# 第三步:关闭文件
file.close()
readline():不带参数时,读取文件中的一行
示例代码:
python
# 第一步:创建文件对象
file = open('a.txt', 'r', encoding='utf-8')
# 第二步:读取文件
print(file.readline())
# 第三步:关闭文件
file.close()
readlines():不带参数时,按行读取完整个文件,返回一个列表
示例代码:
python
file = open('a.txt', mode='r', encoding='utf-8')
print(file.readlines())
file.close()