文章目录
23. Python------读取文件
python修炼第十二天
2019年 3月29日 晴
1.read() 会将所有内容全部读取出来
with open("123.txt") as f:
file_1 = f.read()
print(file_1)
2.read(n) 指定读取的字符数
with open (“123.txt”) as f:
file_1 = f.read(9)
print(file_1)
3.readline() 每次读取一行
with open("123.txt") as f:
while True:
file_1 = f.readline()
if len(file_1) == 0:
break
print(file_1,end = "")
# print()含自动换行(末尾自动加 \n 可以通过end设置参数手段手动设置末尾追加的内容)
4.readlines() 读取出所有内容, 每行是一个元素, 返回列表
with open ("123.txt") as f:
cotent = f.readlines()
print(cotent)
本文介绍了Python中文件的基本读取方法,包括使用read()一次性读取全部内容、read(n)读取指定数量的字符、readline()逐行读取以及readlines()将所有行读取为列表。

被折叠的 条评论
为什么被折叠?



