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写入二进制文件方法的更多相关文章

  1. TypeError: write() argument must be str, not bytes报错

    TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...

  2. Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+

    今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...

  3. 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 ...

  4. TypeError: write() argument must be str, not bytes

    w文件打开以 '二进制'  方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...

  5. 问题记录2:TypeError: write() argument must be str, not bytes

    今天试了下用requests模块的get()方法来下载图片,写入文件的时候不能写入二进制,然后将打开方式改成二进制的就好了. 原因是,f.content的存储方式是二进制,而文件正常打开默认是字符串的 ...

  6. python write() argument must be str, not bytes

    python pickle from __future__ import absolute_import from __future__ import division from __future__ ...

  7. Python - TypeError: unicode argument expected, got 'str'

    参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...

  8. empty(trim($str))报错原因

    最近写程序的时候发现一个这样的问题,一个if判断如下: [php] if (!empty(trim($ch_url))) { ... } [/php] 执行程序报出如下错误: [code] Fatal ...

  9. Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

    python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...

随机推荐

  1. shell和shell脚本基本知识

    详情可见: https://www.cnblogs.com/yinheyi/p/6648242.html 这张图,可以帮助我们理解这个词语! 最底下是计算机硬件,然后硬件被系统核心包住,在系统核心外层 ...

  2. Server SQL Modes

    The MySQL server can operate in different SQL modes, and can apply these modes differently for diffe ...

  3. ubuntu解压和压缩文件

    .tar 解包:tar xvf FileName.tar打包:tar cvf FileName.tar DirName(注:tar是打包,不是压缩!)———————————————.gz解压1:gun ...

  4. fMRI在认知心理学上的研究

    参考:Principles of fMRI 1 问题: 1. fMRI能做什么不能做什么? 第一周:fMRI简介,data acquisition and reconstruction 大致分为两类: ...

  5. 教你一招 - 如何安装nopcommerce2.5

    教你一招 - 如何安装nopcommerce2.5 29. 五月 2012 16:22         /          wcf         /          教你一招 . 解决方案    ...

  6. oracle 查看被锁表 及解除锁定

    查看 哪些表 被锁了 SELECT object_name, machine, s.sid, s.serial# FROM gv$locked_object l, dba_objects o, gv$ ...

  7. logstash配置文件

    1. 安装  logstash 安装过程很简单,直接参照官方文档: https://www.elastic.co/guide/en/logstash/current/installing-logsta ...

  8. CentOS7 安装PHP7的redis扩展:

      phpredis-4.2.0.tar.gz:下载:wget https://github.com/phpredis/phpredis/archive/4.2.0.tar.gz   $ tar -z ...

  9. 『cs231n』卷积神经网络工程实践技巧_下

    概述 计算加速 方法一: 由于计算机计算矩阵乘法速度非常快,所以这是一个虽然提高内存消耗但是计算速度显著上升的方法,把feature map中的感受野(包含重叠的部分,所以会加大内存消耗)和卷积核全部 ...

  10. 用javascript切换bootstrap的tab

    html: <button class="tabContainer" data-toggle="tab" href="#note" i ...