文件操作b模式

#rb
f = open('test1','rb') #b的方式不能指定编码
data = f.read()
#'字符串'----encode------>bytes
#bytes--------decode----->'字符串'
print(data) # 输出结果:b'1111111111\r\n22222\r\n33333333\r\n'
print(data.decode('utf-8')) #编码

#wb
f = open('test22','wb') #b的方式不能指定编码
f.write(bytes('1111111\n',encoding='utf-8'))# 把字符串转换成编码
f.write('22222222222\n'.encode('utf-8')) #和encoding 的效果一样

#ab 再最后一行追加
f = open('test2','ab')
f.write('3杨建'.encode('utf-8'))
posted @ 2018-04-03 18:36  阜阳小全  阅读(176)  评论(0编辑  收藏  举报