TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件:
with open('/python2/random.bin','w') as f:
f.write(os.urandom(10))
但使用Python3会报错:
TypeError:must be str, not bytes
原因为:Python3给open函数添加了名为encoding的新参数,而这个新参数的默认值却是‘utf-8’。这样在文件句柄上进行read和write操作时,系统就要求开发者必须传入包含Unicode字符的实例,而不接受包含二进制数据的bytes实例。
解决方法:
使用二进制写入模式(‘wb’)来开启待操作文件,而不能像原来那样,采用字符写入模式(‘w’)。
同时适配Python3和Python2的方法:
with open('python3/rndom.bin','wb') as f:
f.write(os.urandom(10))
文件读取数据的时候也有类似的问题。解决这种问题的办法也相似:用'rb'模式(二进制模式)打开文件,而不要使用'r'模式。
TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法的更多相关文章
- TypeError: write() argument must be str, not bytes报错
TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...
- Python错误TypeError: write() argument must be str, not bytes
2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not byt ...
- TypeError: write() argument must be str, not bytes
w文件打开以 '二进制' 方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...
- 问题记录2:TypeError: write() argument must be str, not bytes
今天试了下用requests模块的get()方法来下载图片,写入文件的时候不能写入二进制,然后将打开方式改成二进制的就好了. 原因是,f.content的存储方式是二进制,而文件正常打开默认是字符串的 ...
- python write() argument must be str, not bytes
python pickle from __future__ import absolute_import from __future__ import division from __future__ ...
- Python - TypeError: unicode argument expected, got 'str'
参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...
- empty(trim($str))报错原因
最近写程序的时候发现一个这样的问题,一个if判断如下: [php] if (!empty(trim($ch_url))) { ... } [/php] 执行程序报出如下错误: [code] Fatal ...
- Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'
python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...
随机推荐
- linux中tar命令(打包、压缩、解压)、zip和unzip、rar多种压缩文件
一.名词解释 打包:将一大堆文件或目录变成一个总的文件[tar命令] 压缩:将一个大的文件通过一些压缩算法变成一个小文件[gzip,bzip2等] Linux中很多压缩程序只能针对一个文件进行压缩,这 ...
- Spring Boot入门第三天:配置日志系统和Druid数据库连接池。
原文链接 一.日志管理 1.在application.properties文件中加入如下内容: logging.level.root=WARN logging.level.org.springfram ...
- jmap -histo pid 输出的[C [B [I [S 的含义
JMAP 输出 其中: [C is a char[][S is a short[][I is a int[][B is a byte[][[I is a int[][]
- 高并发之限流RateLimiter(二)
Guava RateLimiter提供了令牌桶算法实现:平滑突发限流(SmoothBursty)和平滑预热限流(SmoothWarmingUp)实现. SmoothBursty:令牌生成速度恒定 @T ...
- 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建异步树形菜单
jQuery EasyUI 树形菜单 - 创建异步树形菜单 为了创建异步的树形菜单(Tree),每一个树节点必须要有一个 'id' 属性,这个将提交回服务器去检索子节点数据. 创建树形菜单(Tree) ...
- English trip EM2-LP-1A Hi Teacher:Taylor
课上内容(Lesson) 词汇(Key Word ) Introduce vt. 介绍:引进:提出:采用 greet [ɡrit] vt. 欢迎,迎接:致敬,致意:映入眼帘 n. (Greet ...
- 【源码分析】Mybatis使用中,同一个事物里,select查询不出之前insert的数据
一.问题场景模拟问题:第二次查询和第一次查询结果一模一样,没有查询出我新插入的数据 猜测:第二次查询走了Mybatis缓存 疑问:那为什么会走缓存呢? 1.service方法 @Override @T ...
- PaaS平台型IT运维&运营模式能给企业带来什么?
关注嘉为科技,获取运维新知 什么是PaaS平台型IT自动化运维&运营模式 PaaS平台型IT运维和运维模式是指:将通用的运维能力与具体的运维场景解耦合,将能够复用的,具备独立功能的通用能力纳入 ...
- mac下 配置homebrew 和java home
1.terminal下输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/insta ...
- 谈一谈HashMap类2
1.由一个小案例引出本博文的讨论 public class Demo1 { public static void main(String[] args) throws Exception { Stud ...