2-1 如果只想打印前五行,怎么办?
f = open("donotcry",'r',encoding="utf-8") # 文件句柄
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
# -->
# Talk to me softly. There is something in your eyes
# Don't hang your head in sorrow and please don't cry
# I know how you feel inside I've been there before
# Somethin' is changin' inside you And don't you know
# Don't you cry tonight I still love you baby Don't you cry tonight
# 如果 想读 500 行, copy 500 次?写代码的原则,避免重复代码
2-2 如果只想打印前五行,怎么办?
f = open("donotcry",'r',encoding="utf-8") # 文件句柄
for i in range(5):
print(f.readline())
-->
Talk to me softly. There is something in your eyes
Don't hang your head in sorrow and please don't cry
I know how you feel inside I've been there before
Somethin' is changin' inside you And don't you know
Don't you cry tonight I still love you baby Don't you cry tonight
2-3 现在想把 文件进行 循环;每循环一次,打印一次,到第 10 行的时候,不打印
2-3-1 # 注意 前面的都是 readline(),现在变成 readlines()
f = open("donotcry",'r',encoding="utf-8") # 文件句柄
for line in f.readlines():
# print(line) 有空行,是因为每个后面,默认有 \n
# 如果不想有空行,可以用 strip() 去掉
print(line.strip())
-->
Talk to me softly. There is something in your eyes
……
Tonight
2-3-2
# 第 10 行 不打印 怎么写?
# low loop
f = open("donotcry",'r',encoding="utf-8") # 文件句柄
for index,line in enumerate(f.readlines()):
if index == 9:
print('----- 我是分割线 -----')
continue
print(line.strip())
2-4
# f.readline 只适合读小文件,大文件不合适,会占用内存
# 如果读大文件怎么办?
# 循环一行,删掉一行,内存中只保存一行
# high bige
for line in f:
print(line)
# 这个效率是最高的,因为 f 已经被变成了 迭代器
# 现在他不是列表了,所以,需要弄一个计数器。
count = 0
for line in f:
if count == 9:
print('----我是分割线----')
count += 1
continue
print(line)
count += 1
2-5-1
In [32]: f = open('trial')
In [33]: x = f.read()
In [34]: f.close()
In [35]: x
Out[35]: 'abc\n123\nxyz'
In [36]: f = open('trial')
In [37]: a = f.readline()
In [38]: a
Out[38]: 'abc\n'
In [42]: f.readline()
Out[42]: '123\n'
In [44]: f.readline()
Out[44]: 'xyz'
In [46]: f.readline()
Out[46]: ''
2-5-2
In [54]: f.readlines()
Out[54]: ['abc\n', '123\n', 'xyz']
In [59]: for line in lines:
...: print(repr(line))
...:
'abc\n'
'123\n'
'xyz'
In [60]: for line in lines:
...: print(line.strip())
...:
abc
123
xyz
3-1-1
f = open("donotcry",'r',encoding = "utf-8") # 文件句柄
print(f.tell())
print(f.readline())
print(f.tell())
--->
0
Talk to me softly. There is something in your eyes
52
多少个字符,就默认读多少个数
3-1-3
f = open("donotcry",'r',encoding = "utf-8") # 文件句柄
print(f.tell())
print(f.readline())
print(f.readline())
print(f.tell())
--->
0
Talk to me softly. There is something in your eyes
Don't hang your head in sorrow and please don't cry
105
3-1-4
f = open("donotcry",'r',encoding = "utf-8") # 文件句柄
print(f.tell())
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell())
# 读取三行内容后,想移回光标
# seek 是查找光标
# 如果想回到第二行,回不去的,除非有记录
f.seek(0)
print(f.readline())
# seek 和 tell 搭配使用
--->
0
Talk to me softly. There is something in your eyes
Don't hang your head in sorrow and please don't cry
I know how you feel inside I've been there before
156
Talk to me softly. There is something in your eyes
4-2
# 有读写,就有写读
# f = open("blank",'r+',encoding="utf-8") # 文件句柄 读写
# f = open("blank",'w+',encoding="utf-8") # 文件句柄 写读
# 区别是,写读是先创建一个文件,然后写入
# 先创建文件,写四行
f = open("blank",'w+',encoding="utf-8")
f.write("------- Wow -------1\n")
f.write("------- Wow -------2\n")
f.write("------- Wow -------3\n")
f.write("------- Wow -------4\n")
# 打印位置
print(f.tell())
f.seek(10)
print(f.readline())
f.write("should be at the begining of the second line")
--->
------- Wow -------1
------- Wow -------2
------- Wow -------3
------- Wow -------4
should be at the begining of the second line
# 结果还是追加在后面了,没有办法在中间修改
# 写读模式,没有什么用处
# 读写模式,可以打开,可以追加
4-3-1
# 还有一个追加读
# f = open("blank",'a+',encoding="utf-8") 追加读写
# 追加默认不能读
# f = open("blank",'r+',encoding="utf-8") 用的最多
4-3-2
# 还有一种模式
# f = open("blank",'rb',encoding="utf-8") 以二进制格式读文件
f = open("blank",'rb',encoding="utf-8")
print(f.readline())
print(f.readline())
print(f.readline())
# 里面的不是 二进制 那么能够读取吗
--->
ValueError: binary mode doesn't take an encoding argument
# 二进制的模式是不能传 encoding 参数的
# 因为是 二进制,所以不需要编码了