Python 读写文件 中文乱码 错误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,'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+的更多相关文章
- 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报错
TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...
- TypeError: write() argument must be str, not bytes
w文件打开以 '二进制' 方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...
- TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法
Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...
- 问题记录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 cmd 窗口 中文乱码 解决方法 (附:打印不同颜色)
python cmd 窗口 中文乱码 解决方法 (附:打印不同颜色) 前言 在 python 开发中,有时候想通过cmd窗口来和用户交互,比如显示信息之类的,会比自己创建 GUI 来的方便,但是随之而 ...
- Python 读写文件的正确方式
当你用 Python 写程序时,不论是简单的脚本,还是复杂的大型项目,其中最常见的操作就是读写文件.不管是简单的文本文件.繁杂的日志文件,还是分析图片等媒体文件中的字节数据,都需要用到 Python ...
- 解決BufferedReader读取UTF-8文件中文乱码
解決BufferedReader读取UTF-8文件中文乱码 File rst01 = new File(context.getRealPath("/")+" ...
随机推荐
- python number
一.number类型转换 int(x [,base ]) 将x转换为一个整数 long(x [,base ]) 将x转换为一个长整数 float(x ) 将x转换到一个浮点数 complex(real ...
- OpenCV resources
http://blog.csdn.net/small_foxrabbit/article/details/39858149http://blog.csdn.net/wuyoy520/article/d ...
- STL(标准模板库)基本概念
一.什么是STL STL(Standard Template Library,标准模板库)的从广义上讲分为三类:algorithm(算法).container(容器)和iterator(迭代器),容器 ...
- SVN && BeyondCompare
[1]设置内容 (1)三个步骤对应设置内容 1.1 "D:\Beyond Compare 4\BCompare.exe" %base %mine /title1=%bname /t ...
- python操作Mysql数据库示例
python库:pymysql 安装:install pymysql.mysql数据库 一.连接数据库.创建speder库.查询版本. import pymysql ##链接数据库 db = pymy ...
- python3安装scrapy教程
2.1xm1http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml3. PyOpensslhttps://pypi.python.org/pypi/pyOpen ...
- 【转】ETL讲解(很详细!!!)
ETL是将业务系统的数据经过抽取.清洗转换之后加载到数据仓库的过程,目的是将企业中的分散.零乱.标准不统一的数据整合到一起,为企业的决策提供分析依据. ETL是BI项目重要的一个环节. 通常情况下,在 ...
- 自动添加菜品,加入运行中遇到的异常,生成日志文件...<工作中场景...>
""" 很弱智的小脚本,记录下.也许以后看到会笑,因为太幼稚或者证明曾经也努力过.so... """ """ ...
- Java技术整理1---反射机制及动态代理详解
1.反射是指在程序运行过程中动态获取类的相关信息,包括类是通过哪个加载器进行加载,类的方法和成员变量.构造方法等. 如下示例可以通过三种方法根据类的实例来获取该类的相关信息 public static ...
- Node.js中环境变量process.env详解
Node.js中环境变量process.env详解process | Node.js API 文档http://nodejs.cn/api/process.html官方解释:process 对象是一个 ...