python1

test内容:
hello李杰

test2内容:
国家沦陷只有山河依旧,春日的城区里荒草丛生。


# coding=utf8
# 数据不要放在程序中,放文件中就等于放在磁盘中,不会丢
# 流程:
# 1、打开文件 open()
# 2、如果对文件进行增删改查 readwrite
# 3、关闭文件 close()

# f=open("test") # 把文件从硬件读到内存
# print(f) # 返回文件对象,说明打开了文件
# data=f.read()
# print(data) # 提示gbk报错,说明我的电脑默认是把文件用gbk存到硬盘

# 解决方法一:
# f=open("test",encoding="utf8")
# data=f.read()
# print(data)
# f.close()

# 解决方法二:
# f=open("test2") # 先选GBK保存
# data=f.read()
# print(data)
# f.close()

# 我想读文件一部分
# f=open("test",encoding="utf8")
# data=f.read(5) # python2读是字节,python3读的是字符。python3显示出中文5个字,而python2会乱码
# print(data)
# f.close()

# 读文件就是光标在移动,如果读到最后光标就到最后,如果命令还要求再次读文件,就读不了了。
# f=open("test",encoding="utf8")
# data=f.read()
# data2=f.read()
# print(data)
# print(data2) # 由于光标在最后面了,所以读不了了
# f.close()
#
# f=open("test",encoding="utf8")
# data=f.readline() #打印一行
# print(data)
# f.close()
#
# f=open("test",encoding="utf8")
# print(f.readable()) # 是否可读
# data=f.readlines() #打印所有的行
# print(data)
# f.close()

# 练习:把第3行后面加妈妈

# f=open("test",encoding="utf8")
# i=1
# for line in f.readlines():
# if i==3:
# print(line.strip()+"妈妈")
# line="".join([line.strip(),"岳飞"])
#
# print(line.strip())
# i+=1
# f.close()

# 优化
# f=open("test",encoding="utf8")
# i=1
# for line in f: # 用f代替f.readlines(),结果一样,但是内存占用少,用一行拿一行,拿完后就进垃圾箱
# if i==3:
# print(line.strip()+"妈妈")
# line="".join([line.strip(),"岳飞"])
#
# print(line.strip())
# i+=1
# f.close()

# 如果我们需要可写文件,mode="w"表示可写不可读,如果文件没有就创建,如果有就覆盖,encoding="utf8"表示用utf8编码,从内存写到磁盘
# f=open("test",mode="w",encoding="utf8")

# 试试写操作
# f.write("hello")

# f=open("test",mode="a",encoding="utf8")
# f.write(" world") # 会攥一段时间(运行f.close)才会存到磁盘中

# 停100秒再运行
# import time
# time.sleep(100)

# 要马上写进磁盘
# f.flush()

# 进度条
# import sys
# sys.stdout.write("hello") # 在终端输出hello,等同print

# import sys
# for i in range(100):
# sys.stdout.write("#") # 等同print
# sys.stdout.flush() # 刷新
#
# import time
# time.sleep(0.1) # 0.1秒后才下次循环

# f=open("test",mode="x",encoding="utf8") # x模式就是覆盖会提醒
# f.write("hello")

# 可读可写模式 r+ w+ a+
# r+ 读,可追加写内容
# f=open("test",mode="r+",encoding="utf8") # x模式就是覆盖会提醒
# print(f.read())
# f.write("where is he?")
# print(f.read())
# f.close()

# w+首先覆盖变没内容,所以没内容
# f=open("test",mode="w+",encoding="utf8") # x模式就是覆盖会提醒
# print(f.read())
# f.write("你是谁where is he? ")
# print(f.read()) # 没内容是因为光标在后面,要调光标
# f.seek(0) #将光标移到开始位置
# print(f.read(4)) #按字符走,所以中文不会出错
# f.seek(4) #将光标移到第4个字节的位置,按字节走,所以中文出错
# print(f.tell()) # 打印光标的位置
# print(f.read())


# a+总是在最后添加,不会覆盖
# f=open("test",mode="a+",encoding="utf8")
# f.seek(0)
# print(f.read())
# f.write("yuan嘻嘻")
# print(f.read())
# f.seek(0)
# f.write("xie") #虽然光标在前面,但是还是在最后面添加新的内容,因为之前内容已经在磁盘中,不能插内存

# r读 w覆盖写 a追加(基于字符操作的,rb wb ab是基于字节来操作的)
# r+:默认光标在开始位置,追加写
# w+:覆盖写,想读取内容,seek调整
# a+:光标默认在文件最后位置,不管光标在哪里,都一定是最后面追加内容,想读取内容就用seek调整

# seek的应用:比如断点续传(上传过程中断网了,要继续传剩下的)

# f=open("test","rb") # 读二进制的内容,不用解码
# print(f.read())


# f=open("test","wb") #
# f.write("hello李杰".encode("utf8"))
# f.close()

# with open("test") as f: # f=open...,这样的好处是缩进,清晰
# f.read()

原文地址:https://www.cnblogs.com/jensenxie/p/8457752.html