Python 读写
读:read(), read(size), readlines()
写:write()
关闭 close()
StingIO, BytesIO()
读文本文件
read()
f = open('D:/test.txt', 'r') # 'r'表示读文件
f.read() #调用read()方法一次性读取文件的内容
f.close() #关闭文件
通过with方法,不用关闭文件,执行完毕自动关闭
with open('D:/test.txt', 'r') as f:
print(f.read())
with open('D:/test.txt', 'r')as f:
for line in f.readlines():
print(line)
读取二进制文件,比如图片,视频,要用‘rb’模式打开
with open('C:/Users/Public/Pictures/Sample Pictures/aa.jpg', 'rb') as f:
print(f.read(100))
读取非UTF-8编码的文件,在open方法里加上参数encoding = ''
如读取GBK编码的文件:
with open('C:/Users/Public/Pictures/Sample Pictures/gbk.jpg', 'r', encoding = 'GBK') as f:
print(f.read(100))
写文件write()
注意注意:这种写法会把文件里原来的内容删除
with open('D:/test.txt', 'w') as f:
f.write('test write')
在文件里添加内容的写法是:传入参数‘a’代替‘w’
with open('D:/test.txt', 'a') as f:
f.write('test write')
StringIO就是内存中读写str
用write()方法写入,然后必须用getvalue()方法读取
f = StringIO()
f.write('hello')
f.write(' ')
f.write('world')
print(f.getvalue()) BytesIO用来操作二进制数据
f = BytesIO()
f.write('刘数量'.encode('utf-8'))
print(f.getvalue()) f = BytesIO(b'\xe5\x88\x98\xe8\x8b\xb1')
print(f.read())
Python 读写的更多相关文章
- Python读写文件
Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...
- python 读写、创建 文件
python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- [Python]读写文件方法
http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- 使用Python读写csv文件的三种方法
Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...
- python读写word、excel、csv、json文件
http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...
- python读写csv文件
文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...
- python 全栈开发,Day86(上传文件,上传头像,CBV,python读写Excel,虚拟环境virtualenv)
一.上传文件 上传一个图片 使用input type="file",来上传一个文件.注意:form表单必须添加属性enctype="multipart/form-data ...
- Python读写docx文件
Python读写word文档有现成的库可以处理.我这里采用 python-docx.可以用pip install python-docx安装一下. 这里说一句,ppt和excel也有类似的库哦,而且是 ...
随机推荐
- vivado对task和function的可综合支持
手册UG901,对vivado可综合的语句支持进行了描述,HDL包括:verilog-2001,system-verilog,VHDL: verilog-2001扩展了对task和function的支 ...
- Android开发 ---Fragment片段布局
前言 Fragment想必大家不陌生吧,在日常开发中,对于Fragment的使用也很频繁,现在主流的APP中,基本的架构也都是一个主页,然后每个Tab项用Fragment做布局,不同选项做切换,使用起 ...
- easyui弹框后销毁当前tab弹框不显示的解决方式
var id=$("#pageId").val(); var message = "{\"id\":" + id+ ",\&quo ...
- 关于urls 的基础
1 普通正则 2 分组正则 url(r'/blog/(\d+)/(\d+)',views.blog) blog(request,arq1,arq2) 按照位置传参 3 分组命名 url(r'/ ...
- MySQL:数据表基本操作
数据表基本操作 注意点: 1.数据表中已经有数据时,轻易修改数据类型,有可能因为不同的数据类型的数据在机器 中存储的方式及长度并不相同,修改数据类型可能会影响到数据表中已有的数据类型. 2. 数据表 ...
- ueditor 设置高度height. ue.setHeight(400); 设置宽度 width
1.引入的文件: <script type="text/javascript" src="../../dist/ueditor1_4_3-utf8-php/uedi ...
- 6--Python入门--Python基本运算符
算数运算符 运算符 描述 示例 + 相加 1+1→2 - 相减 1-1→0 * 相乘 1*2→2 / 相除 1/2→0.5 % 取余数 3%2→1 ** 幂运算 2**2→4 // 取商 7//2→3 ...
- 17 多校1 Add More Zero 水题
Problem Description There is a youngster known for amateur propositions concerning several mathemati ...
- Pycharm出现Segmentation fault...(interrupted by signal 11: SIGSEGV)的解决方法
众所周知,用pycharm远程服务器debug代码已经成为学习深度学习相关代码的有力工具,但是最近创建了一个虚拟环境,进行debug的时候,莫名会出现下面这个错误,看的我都抽风了 bash: line ...
- [LeetCode&Python] Problem 717. 1-bit and 2-bit Characters
We have two special characters. The first character can be represented by one bit 0. The second char ...