Python ---------- 文件操作

"""
fp = open("文件名",mode="模式",encoding="utf-8")
fp -> 文件的io对象 (文件句柄)
i => input  输入
o => output 输出
"""

# 一.文件的写入
# 1.打开文件
fp = open("ceshi1.txt",mode="w",encoding="utf-8") # 把冰箱门打开

# 2.写入内容
fp.write("把大象塞进去") # 把大象放进去

# 3.关闭文件
fp.close() # 把冰箱门关上


# 二.文件的读取
# 1.打开文件
fp = open("ceshi1.txt",mode="r",encoding="utf-8") #1.打开冰箱门

# 2.读取内容
res = fp.read() # 把大象拿出来
print(res)

# 3.关闭文件
fp.close() # 把冰箱门关上


# 三.字节流的转换
"""
bytes : 是用来传输或者存储的数据格式
b'1234'  b"abcd" b"我爱你" -> b开头的字节流,范围只能是ascii编码

如果是中文使用encode 和 decode 来进行转换;
# 将字符串和字节流(Bytes流)类型进行转换 (参数写成转化的字符编码格式)
    #encode() 编码  将字符串转化为字节流(Bytes流)
    #decode() 解码  将Bytes流转化为字符串
"""
a = b'1234'
print(a , type(a))

strvar = "我爱你"
# encode 编码 -> 变成二进制字节流
res = strvar.encode("utf-8")
print(res , type(res))

# decode 解码 =>二进制字节流恢复成原来的字符串
res2 = res.decode("utf-8")
print(res2 , type(res2))

# len可以计算字节个数
num = len(res)
print(num) # 9

# 一个中文占用3个字节,通过decode 反解出爱这个字
res3 = b"\xe7\x88\xb1".decode("utf-8")
print(res3)

#  程序员的表白方式
strvar = "我!是你一辈子也得不到的男人"
strvar2 = strvar.encode()
print(strvar2)


# 四.存储二进制字节流
"""不需要指定encoding编码集,否则报错"""
fp = open("ceshi2.txt",mode="wb")
fp.write(strvar2)
fp.close()


# 五.读取二进制字节流
fp = open("ceshi2.txt",mode="rb")
res = fp.read()
fp.close()

print(res)
# 通过decode反解出字符串
strvar = res.decode()
print(strvar)

# 六.复制图片
"""图片,音频,视频"""
# 1.读取原图片所有的内容
fp = open("集合.png",mode="rb")
res = fp.read()
fp.close() 

# 2.把读取的内容存储到另外一个文件
# fp = open("集合2.png",mode="wb")
# 指定绝对路径(完整路径)
fp = open(r"E:\python31\day8\集合3.png",mode="wb")
fp.write(res)
fp.close()

扩展模式

# ### 文件的扩展模式
# (utf-8编码格式下 默认一个中文三个字节 一个英文或符号 占用一个字节)
    #read()		功能: 读取字符的个数(里面的参数代表字符个数)
    #seek()		功能: 调整指针的位置(里面的参数代表字节个数)
    #tell()		功能: 当前光标左侧所有的字节数(返回字节数)
"""
seek(0)   直接把光标移动到文件开头
seek(0,2) 直接把光标移动到文件末尾
"""

# 一.文件的扩展模式
# r+ 先读后写
"""
fp = open("ceshi3.txt",mode="r+",encoding="utf-8") 
# 先读
res = fp.read()
print(res)

# 后写
fp.write("1234")

# 在读
fp.seek(0) # 调整光标位置在开头
res = fp.read()
print(res)
fp.close()
"""

# r+ 先写后读
"""
fp = open("ceshi3.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("ceshi4.txt",mode="w+",encoding="utf-8")
fp.write("abc")

fp.seek(0)
res = fp.read()
print(res)
fp.close()
"""
# a+ 可读可写
"""文件不存在时,默认创建新的文件"""
"""
fp = open("ceshi5.txt",mode="a+",encoding="utf-8")
fp.write("123")

fp.seek(0)
res = fp.read()
print(res)

# a模式在写入内容时,会强制把光标移动到最后
fp.seek(1)
fp.write("abc")

fp.close()


# 如果在r模式内,区别a模式
fp = open("ceshi5.txt",mode="r+",encoding="utf-8")
fp.seek(1)
fp.write("abc")
fp.close()
"""

# 二.read  seek tell 三个函数的使用
"""
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
res = fp.read(3)
print(res)

fp.seek(6)
print(fp.read(1))

# 计算文件指针左侧所有的字节数
res = fp.tell()
print(res)
fp.close()
"""

# 注意点,seek移动中文字节的时候,有可能报错
"""
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.seek(2)
res = fp.read()
print(res)
fp.close()

# print("我".encode())
# b'\xe6\x88\x91'
"""

# 三.with语法的使用 (close操作with语法可以自动实现)
with open("集合.png",mode="rb") as fp:
	res = fp.read()

with open(r"E:\python31\day8\集合4.png",mode="wb") as fp:
	fp.write(res)

# 继续优化 合并with
with open("集合.png",mode="rb") as fp1 ,  open(r"E:\python31\day8\集合5.png",mode="wb") as fp2:
	res = fp1.read()
	fp2.write(res)

相关函数

# ### 1.close 文件关闭的意义
"""
# 刷新缓冲区 flush
    # 当文件关闭的时候自动刷新缓冲区
    # 当整个程序运行结束的时候自动刷新缓冲区
    # 当缓冲区写满了  会自动刷新缓冲区
    # 手动刷新缓冲区
fp = open("ceshi6.txt",mode="r+",encoding="utf-8")
fp.write("zzz")
# 手动把缓冲区里面的内容写入文件当中
fp.flush()



fp.close()
"""


# ### 2.文件的相关函数
fp = open("ceshi6.txt",mode="a+",encoding="utf-8")
#readable()	    功能: 判断文件对象是否可读
res = fp.readable()
print(res)
#writable()	    功能: 判断文件对象是否可写
res = fp.writable()
print(res)


#readline()     功能: 读取一行文件内容
"""
参数 > 当前行字符总个数 => 以当前行读取
参数 < 当前行字符总个数 => 以参数的大小来读取字符的个数

默认readline 读取一行
"""

"""
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:	
	# res = fp.readline(3000)
	# print(res)
	
	# 文件对象fp也是一个可迭代对象
	'''在遍历文件对象的时候,默认一次拿一行'''
	for i in fp:
		print(i)

# 读取所有内容
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:	

	# 先读取一行
	res = fp.readline()
	# 判断是不是空
	while res:
		print(res)
		res = fp.readline()

"""

#readlines()    功能:将文件中的内容按照换行读取到列表当中
lst_new = []
with open("ceshi7.txt",mode="r+",encoding="utf-8") as fp:	
	lst = fp.readlines()
	# print(lst) # ['\t窗前明月光\n', '疑是鞋两双\t\t\n', '\t\t举头王明月\n', '\t低头看裤裆']
	for i in lst:		
		lst_new.append(i.strip())

print(lst_new)	 # ['窗前明月光', '疑是鞋两双', '举头王明月', '低头看裤裆']


#writelines()   功能:将内容是字符串的可迭代性数据写入文件中 参数:内容为字符串类型的可迭代数据
"""可迭代型数据(容器类型数据,range对象,迭代器)"""
"""
lst = ["春眠不觉晓\n","处处蚊子咬\n","夜来大狗熊\n","一个也跑不了\n"]
# lst = [1,2,3,4] error
with open("ceshi8.txt",mode="w",encoding="utf-8") as fp:
	fp.writelines(lst)
"""

#truncate()     功能: 把要截取的字符串提取出来,然后清空内容将提取的字符串重新写入文件中 (字节)
with open("ceshi8.txt",mode="r+",encoding="utf-8") as fp:
	fp.truncate(3)

while " ":
	print(123)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值