手稿.10.27

f = open("a1.txt", mode="r", encoding="utf-8")
content = f.read()

print(content)

2
f2= open(r"G:\课件\day08 文件操作\作业\a1.txt",mode = "r",encoding = "utf-8")
print(f2.read(6))

line=f2.readline() # f.readline() 读取一行
print(line.strip())
line=f2.readline() # 两行同样的读取,第一行read了,下面同样的代码会read第二行
print(line.strip())

line = f2.readlines() #f.readlines()读取全部,变成一行的列表
print(line)
f.readlinessssss() 不能加.strip去括号

但是一行一行打太麻烦了,让它循环打吧,想到无线循环while 1
f2= open(r"G:\课件\day08 文件操作\作业\a1.txt",mode = "r",encoding = "utf-8")
while 1:
line= f2.readline()
print(line)

f2= open(r"G:\课件\day08 文件操作\作业\a1.txt",mode = "r",encoding = "utf-8")
for line in f2: # 读取文件的内容,for i in f2, 一行一行读取.
print(line.strip()) # for line in f2,也可以用strip去空格
句柄也是可以迭代的对象,
可以用for迭代,.strip() 去空格

f3 = open(r"G:\课件\day08 文件操作\作业\a11.txt",mode="w",encoding="utf-8")
f3.write("\n")
f3.write('喵森\n')
f3.write("你是\n")
f3.write("最棒的\t")
f3.write("相信\n")
f3.close
posted @ 2018-10-27 16:28  Jack的逆旅  阅读(113)  评论(0编辑  收藏  举报