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. MySQL official tutorial

    1.installation 2.setup environment variables add %/MySQL Server/bin to path. then restart cmd/powers ...

  2. 雷林鹏分享:查看 XML 文件

    查看 XML 文件 在所有主流的浏览器中,均能够查看原始的 XML 文件. 不要指望 XML 文件会直接显示为 HTML 页面. 查看 XML 文件 - Tove Jani Reminder Don' ...

  3. LeetCode--014--最长公共前缀(java)

    编写一个函数来查找字符串数组中的最长公共前缀. 如果不存在公共前缀,返回空字符串 "". 示例 1: 输入: ["flower","flow" ...

  4. 树莓派 CSI摄像头 No data received from sensor. Check all connections, including the Sunny one on the camera board

    不知道为什么摄像头在包里放了两天旧坏了,中间完全没用过摄像头的功能,查了资料,原因大概有两种 1)sunny烧了 2)试摄像头传感器坏了 这两天没有插拔过摄像头,可能是树莓派漏电了,也可能是它被压坏了 ...

  5. 如何设置Git SSH密钥

    1. SSH 存储在user/用户名/.ssh文件夹下 生成SSH密钥 $ ssh-keygen -t rsa -C "your_email" 2. 查看生成的公钥 $ cat ~ ...

  6. JS代码判断IE6,IE7,IE8,IE9

    做网页有时候会用到JS检测IE的版本,下面是检测Microsoft Internet Explorer版本的三种代码! 有一种代码: <script type="text/javasc ...

  7. hbase安装部署

    hbase的安装 ①cp /mnt/hgfs/xiazai/hbase-1.2.5-bin.tar.gz /data tar -xzvf  hbase-1.2.5-bin.tar.gz ②环境 sud ...

  8. leetcode-algorithms-15 3Sum

    leetcode-algorithms-15 3Sum Given an array nums of n integers, are there elements a, b, c in nums su ...

  9. 基于jquery实现页面loading加载效果

    实现loading 加载提示 ······ 透明遮罩 居中效果 具体代码如下: CSS样式部分: <style type="text/css"> .background ...

  10. C++的面试题

    1.什么是类? 类是具有相同属性和相同的方法的对象的集合,它是一种既包含数据又包含函数的抽象数据类型. 对象是类进行实体化后的产物,是一个实体. 在C++中也是先声明一个类类型,用类去定义若干个同类型 ...