今天写上传文件代码,如下

def uploadHandle(request):
pic1=request.FILES['pic1']
picName=os.path.join(settings.MEDIA_ROOT,pic1.name)
with open(picName,'w') as pic:
for c in pic1.chunks():
pic.write(c)
return HttpResponse(picName)

出现TypeError: write() argument must be str, not bytes错误

网上搜索才发现原来是文件打开方式有问题,把之前的打开语句修改为用二进制方式打开就没有问题

改为:

def uploadHandle(request):
pic1=request.FILES['pic1']
picName=os.path.join(settings.MEDIA_ROOT,pic1.name)
with open(picName,'wb+') as pic:
for c in pic1.chunks():
pic.write(c)
return HttpResponse(picName)

产生问题的原因是因为pickle存储方式默认是二进制方式

Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+的更多相关文章

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

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

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

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

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

  4. TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法

    Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...

  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 cmd 窗口 中文乱码 解决方法 (附:打印不同颜色)

    python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色) 前言 在 python 开发中,有时候想通过cmd窗口来和用户交互,比如显示信息之类的,会比自己创建 GUI 来的方便,但是随之而 ...

  8. Python 读写文件的正确方式

    当你用 Python 写程序时,不论是简单的脚本,还是复杂的大型项目,其中最常见的操作就是读写文件.不管是简单的文本文件.繁杂的日志文件,还是分析图片等媒体文件中的字节数据,都需要用到 Python ...

  9. 解決BufferedReader读取UTF-8文件中文乱码

    解決BufferedReader读取UTF-8文件中文乱码         File rst01 = new File(context.getRealPath("/")+" ...

随机推荐

  1. 将NetBIOS名称解析为IP地址的常用方法

    在Windows网络中,当一台计算机要利用NetBIOS名称与另一台计算机通信时,首先要将对方计算机的NetBIOS名称解析成IP地址 2.广播   通过发送一个广播消息来查询对方的IP地址,拥有此N ...

  2. Spark以yarn-client提交任务时报错超时,Connection to 192.168.. /has been quiet forms while there are outstanding requests. Failed to send RPC.....

    报错信息如上,具体是运行FusionInsight给的样例SparkPi,在local环境下是可以的,但是如果以yarn-client模式就会卡住,然后120s以后超时,其实以yarn-cluster ...

  3. kafka 单机配置

    http://blog.csdn.net/jingshuigg/article/details/24439637 . su root : chown -R hadoop:hadoop version- ...

  4. meta twitter 属性

    总结下国际范儿的meta标签 <meta name="A game made to inspire developers to use GSAP, ES6 and Flexbox&qu ...

  5. html5-特殊符号的使用

    <!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  6. hdu4787 AC自动机加分块

    这题说的是 有n次操作 +w 表示读入一个字符串,?p 询问这个字符串的子串在那些模板串中有多少个, http://blog.csdn.net/qq574857122/article/details/ ...

  7. IO model

    上节的问题: 协程:遇到IO操作就切换. 但什么时候切回去呢?怎么确定IO操作完了? 很多程序员可能会考虑使用“线程池”或“连接池”.“线程池”旨在减少创建和销毁线程的频率,其维持一定合理数量的线程, ...

  8. win10安装mongodb-win32-x86_64-2008plus-ssl-3.4.10-signed

    1.下载mongodb在windows下的安装文件 首先去官网https://www.mongodb.com/download-center?jmp=nav#community下载安装文件.mongo ...

  9. Python 连接SQL Server数据库 - pymssql使用基础

    1.  官方api http://www.pymssql.org/en/stable/ref/pymssql.html 我学习自这里

  10. Failed to start component [StandardEngine[Tomcat].StandardHost[localhost]]

    1.问题场景描述:一个maven项目启动时候,偶尔会报tomcat的这个错误(如图:) 随机报错,有时频率很高,要一直重新启动很多次可能还是启动不了,有时不报错.. 2.解决过程:网上各种寻找解决办法 ...