python小白——进阶之路——day8天-———文件的相关操作以及文件函数

###文件操作

'''
fp = open("文件名",mode="采用的模式",encoding="使用什么编码集")
fp 这个变量会接受到 open的返回值 是一个文件io对象 (又称文件句柄)
i => input 输入
o => output 输出
有了对象之后,就可以使用对象.属性 或者 对象.方法进行操作.

fp.write("字符串")
fp.close()
fp.read() 读取内容
'''
#(1)文件的写入和读取
#写入文件
'''
#打开文件
fp = open("0506.txt",mode="w",encoding="utf-8")#把冰箱门打开
#写入文件
fp.write("把大象放到冰箱里面")#把大象塞进去
#关闭文件
fp.close()#把冰箱门关上
'''
'''
# 打开文件
fp = open("0506.txt",mode="r",encoding="utf-8")# 把冰箱门打开
# 读取文件
res = fp.read()# 把大象从冰箱里面拿出来
# 关闭文件
fp.close()# 把冰箱门关上
print(res)
'''
# 将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式)
#encode() 编码 将字符串转化为字节流(Bytes流)
#decode() 解码 将Bytes流转化为字符串
strvar = "你好"
by = strvar.encode("utf-8")
print(by)
res2 =by.decode("utf-8")
print(res2)

# (2) wb 和 rb 模式 二进制字节流模式 (注意点:使用字节流模式的时候,不要指定encoding)
#文件的写入
fp = open("0506_2.txt",mode="wb")
strvar = "你好啊"
res = strvar.encode("utf-8")
fp.write(res)
fp.close()
#文件的读取
fp = open("0506_2.txt",mode="rb")
res = fp.read()
fp.close()
res2 =res.decode("utf-8")
print(res2)
# (3)复制图片(图片或者视频之类的二进制字节流的内容, 都可以使用b模式
# 比如wb, rb..)
#打开文件
fp = open("ceshi.png",mode="rb")
#读取文件
res = fp.read()
#关闭文件
fp.close()

#打开文件
fp.open("ceshi2.png",mode="wb")
#写入文件
fp.write()
#关闭文件
fp.close()



###扩展模式 + w+ r+ a+
'''
#(utf- 8 编码格式下 默认一个中文桑字节 一个英文或者符号一个字节)
# read() 功能:读取字符的个数(里面的参数代表字符个数)
#seek() 功能: 调整指针的位置(里面的参数代表字节个数)
#tell() 功能: 当前光标左侧所有的字节数(返回字节数)

把光标移动到文件行首
fp.seek(0)
把光标移动到文件末尾
fp.seek(0,2)
'''
# r+ 可读可写 (先读后写)
'''
fp = open("0506_3.txt",mode="r+",encoding="utf-8")
res = fp.read()
# print(res)
fp.write("e")
#把光标移动到第0个字节上,那就是文件开头
fp.seek(0)
res= fp.read()
print(res)
fp.close()
'''
# r+ 可读可写( 先写后读)
'''
fp = open("0506_3.txt",mode="r+",encoding="utf-8")
#把光标移动到文件末尾
fp.seek(0,2)
fp.write("123")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
'''
# w+ 可读可写
'''
fp = open("0506_4.txt",mode="w+",encoding="utf-8")
fp.write("123123")
fp.seek(0)
res = fp.read()
print(res)
fp.close()
'''
#a+ 可读可写 append(写入的时候,强制把光标移动到最后)
'''
fp = open("0506_4.txt",mode="a+",encoding="utf-8")
fp.seek(0)
fp.write("999")
fp.seek(0)
res =fp.read()
print(res)
fp.close()
'''
# tell read seek 注意 seek括号里的是字节数 read括号里的是字符数
'''
fp = open("0506_4.txt",mode="a+",encoding="utf-8")
res = fp.tell()
print(res)
fp.seek(5)
res = fp.tell()
print(res)
res = fp.read(2)#读取2个字符
res =fp.tell()
print(res)
fp.close()
'''
# 注意有中文的情况 , 如果移动错误,有可能读取不出来.
'''
fp = open("0506_4.txt",mode="a+",encoding="utf-8")
res = fp.tell()
print(res)
fp.seek(2)
res = fp.tell()
print(res)
fp.read(2)
res = fp.tell()
print(res)
# 当前后面的字符是天 如果用seek移动1个字节
fp.seek(5)
res = fp.tell()
print(res)
res = fp.read()
fp.close()

# \xa1\xa2\xa3


'''
# with 操作
# with 语法 自动关闭文件 相当于帮你执行了fp.close()
# as 相当于起别名 fp = open("0506_5.txt",mode="w",encoding="utf-8") with 是一个代码块 不要落下冒号:
with open("0506_5.txt",mode="w",encoding="utf-8") as fp:
# 往文件里面写的内容只能是字符串 或者 字节流
fp.write("123")


###(1)flush
#刷新缓冲区 flush
#当文件关闭的的时候自动刷新缓冲区
#当这个程序运行结束的时候自动刷新缓冲区
#当缓冲区写满了,会自动刷新缓冲区
#手动刷新缓冲区
'''
fp = open("0506_6.txt",mode="w+",encoding="utf-8")
fp.write("123456")
#手动刷新缓冲区
fp.flush()
while True:
pass
fp.close()
'''
#(2)文件对象具有可迭代性
#readable 功能:判断文件对象是否可读
#writable 功能:判断文件对象是否可写
'''
fp = open("0506_6.txt",mode="r",encoding="utf-8")
res1 = fp.readable()
res2 =fp.writable()
print(res1)#True
print(res2)#False
for i in fp:
print(i)
'''









#(3)文件的相关函数
#readline() 功能: 读取一行文件内容
'''
readline(字符数)
如果给的参数大于当前行字符数,只获取当前行所有内容
如果给的参数小于当前行字符数,按照实际给的参数进行字符的获取
'''

with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
res = fp.readline(300)
print(res)
# 0 0.0 0j False '' [] () set() {} None
# 循环打印文件里面每一行的内容
while res:
print(res)
res = fp.readline()


# readlines() 功能:将文件中的内容按照换行读取到列表当中
'''
lst=[]
with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
res = fp.readlines()
for i in res:
res2= i.strip()
lst.append(res2)
#print(res)
print(lst)
'''
#writelines() 功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据
with open("0506_6.txt",mode="r+",encoding="utf-8") as fp:
strvar= "今天天气好晴朗"
lst = ["亲爱的\n","美女\n","请看我\n","一眼\n"]
fp.writelines(lst)
# truncate() 功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)
with open("0506_6.txt", mode="r+", encoding="utf-8") as fp:
fp.truncate(6)

"""
read(字符)
readline(字符)
seek(字节)
truncate(字节)
tell返回的是字节
"""








猜你喜欢

转载自www.cnblogs.com/-sch-593057927/p/10854191.html