标签:src mode with open chat unicode open 哈哈 lin rip
# Python learning
# coding:utf-8
"""
t:
1.读写都是以字符串(unicode)为单位
2.只能针对文件
3.必须指定字符编码,即必须指定encoding参数
b:binary模式
1.读写都是以bytes为单位
2.可以针对所有文件
3.一定不能指定字符编码,即不能指定encoding参数
"""
# with open(r"a.txt", mode="rb") as f:
# res = f.read()
# print(res, type(res))
# print(res.decode(‘utf-8‘))
#
# with open(r"a.txt", mode="wb") as f:
# res = f.write("哈哈哈haha".encode("utf-8"))
#
# with open(r"a.txt", mode="ab") as f:
# res = f.write("哈哈哈haha".encode("utf-8"))
# 文件copy工具
src_file = input("path1:").strip()
dst_file = input("path2:").strip()
with open(r‘{}‘.format(src_file), mode=‘rb‘) as f1, \
open(r‘{}‘.format(dst_file), mode=‘wb‘) as f2:
# 方式1:
# for line in f1:
# res = line
# f2.write(res)
# 方式2:
while True:
res = f1.read(1024)
if len(res) == 0:
break
else:
f2.write(res)
# with open(r"WeChat Image_20210314231916.jpg", mode="rb") as f:
# while True:
# res = f.read(1024)
# if len(res) == 0:
# break
# print(len(res))
标签:src mode with open chat unicode open 哈哈 lin rip
原文地址:https://www.cnblogs.com/wyless/p/14930435.html