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 ...
随机推荐
- (转)基于C#的socket编程的TCP异步实现
一.摘要 本篇博文阐述基于TCP通信协议的异步实现. 二.实验平台 Visual Studio 2010 三.异步通信实现原理及常用方法 3.1 建立连接 在同步模式中,在服务器上使用Accept方法 ...
- arcgis 浅入
首先声明,此文只是用于学习,非商业用途!!20181226谷子弟留 有朋友需要用arcgis来学习分析图块,实现图块的分类和数据分析和统计. 于是网上找了找资源. http://pan.baidu ...
- 自动化部署之jenkins及简介
一.什么是持续集成? (1)Continuous integration(CI) 持续集成是一种软件开发实践,即团队开发成员经常集成他们的工作,通常每个成员至少集成一次,也就意味着每天可能会发生多次集 ...
- 二、idea + git
1.配置git file->setting->git Test 2.配置gitHub 2.1 生成gitHub settings->Developer settings->P ...
- datagrid复制
private void Dgv_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar == (char)1) // Ctrl-A ...
- 怎么从bam文件中提取出比对OR没比对上的paired reads | bamToFastq | STAR
折腾这么多都是白瞎,STAR就有输出没有别对上的pair-end reads的功能 参见:How To Filter Mapped Reads With Samtools I had the same ...
- github第一步之初始化操作
目录 0.首先注册一个账号 1.创建知识库Repository 2.创建一个分支branch--feature 3.制作并提交commit 4.打开拉取请求pull 5.合并自己的pull请求 git ...
- mac终端下连接阿里云服务器
通过ssh连接 ssh 用户名@地址 ssh root@xx.xxx.xxx.xx https://www.jianshu.com/p/f034817a7837 最后还是通过 FileZilla 连 ...
- 突破本地离线存储5M限制的JS库localforage简介
http://www.zhangxinxu.com/wordpress/2018/06/js-localforage-localstorage-indexdb/
- 2018"百度之星"程序设计大赛 - 资格赛 A/B/E/F
调查问卷 Accepts: 505 Submissions: 2436 Time Limit: 6500/6000 MS (Java/Others) Memory Limit: 262144/ ...