文件的读取
1、read方法
with open('test.txt', 'r', encoding='utf-8') as file:
x = file.read()
print(x)
with open('test.txt', 'r', encoding='utf-8') as file:
x = file.read(10)
print(x)
2、for循环
with open('test.txt', 'r', encoding='utf-8') as file:
for x in file:
print(x)
3、readline方法
with open('test.txt', 'r', encoding='utf-8') as file:
line = file.readline()
print(line)
with open('test.txt', 'r', encoding='utf-8') as file:
line = file.readline(3)
print(line)
with open('test.txt', 'r', encoding='utf-8') as file:
line = file.readline()
print(line)
line = file.readline()
print(line)
line = file.readline()
print(line)
4、readlines方法
with open('test.txt', 'r', encoding='utf-8') as file:
line = file.readlines()
print(line)
with open('test.txt', 'r', encoding='utf-8') as file:
line = file.readlines(25)
print(line)