二进制文件
- 1.二进制读取模式
- `rb`读取模式
- 将读取到的内容写入到文件
1.二进制读取模式
-
rt
读取文本文件(默认值) -
rb
读取二进制文件
file_name = "C:/Users/cheng/Desktop/can.jmx"
with open(file_name, 'rt', ) as can:
print(can.read())
执行结果
rb
读取模式
file_name = "C:/Users/cheng/Desktop/can.jmx"
with open(file_name, 'rb', ) as can:
print(can.read())
执行结果
- 读取100字节
file_name = "C:/Users/cheng/Desktop/can.jmx"
with open(file_name, 'rb', ) as can:
#读取文本文件,size是以字符为单位的
#读取二进制文件,size是以字节为单位的
print(can.read(100))
执行结果
将读取到的内容写入到文件
file_name = "C:/Users/cheng/Desktop/can.jmx"
with open(file_name, 'rb', ) as can:
# 定义一个新的文件
new_name = 'to.jmx'
with open(new_name, 'wb') as cheng:
# 定义每次读取的大小
cc = 1024 * 100
while True:
# 从已有对象中读取数据
content = can.read(cc)
#内容读取完毕,终止循环
if not content:
break
#将读取到的数据写入到新对象中
cheng.write(content)
执行结果: