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也有类似的库哦,而且是 ...
随机推荐
- IDEA中maven的依赖jar包报红
问题描述: 查看本地的repository中有相关的jar包,但是在IDEA中就是报红(下面红色波浪线) 解决方法: 将pom.xml中相关的dependcy配置注释掉,maven中jar包就会删除. ...
- 记录搭建ssm项目
搞java也快3年了,搭建一个ssm居然有点吃力. 参考链接:https://blog.csdn.net/gebitan505/article/details/44455235/ 环境准备:jdk8. ...
- shell脚本实例-安装httpd,安装yum源
1.安装httpd #!/usr/bin/bash getway=192.168.1.1 ping -c1 www.baidu.com &>/dev/null if [ $? -eq 0 ...
- python 正则进阶常用方法
表达式 描述 正则表达式示例 符号 literal 匹配文本字符串的字面值literal foo rel1|rel2 匹配正则表达式rel1或rel2 foo|bar . 匹配任何字符(除了\n之外) ...
- 使用pyspider爬取巨量淘宝MM图片
具体搭建步骤不再赘述,这里主要使用到了fakeagent,phantomjs和proxy pyspider的爬取相当智能,在不能获取图片的时候会适当的暂停一段时间再试探性的爬取,配合fakeagent ...
- 如何通过创建切片器窗格节省PowerBI报告空间
许多用户在使用Power BI的过程中,都会有这么一个困扰:在Power BI 开发中,切片器一旦过多就会占用非常多的空间.发生这种情况时,您显示数据的页面也会更加小.但另一方面,如果您没有切片器,报 ...
- 【转】IPV6的地址类型
http://blog.sina.com.cn/s/blog_8d795a0f01018hiz.html <IPV6的地址类型>IPV6的地址类型 可分为三大类: 1.单播地址 2.组播地 ...
- https://stackoverflow.com/questions/40949967/running-storm-from-intellij-nimbus-error
https://stackoverflow.com/questions/40949967/running-storm-from-intellij-nimbus-error 0down votefavo ...
- 【转载】 OpenCV ——双线性插值(Bilinear interpolation)
原文地址: https://www.cnblogs.com/yssongest/p/5303151.html --------------------------------------------- ...
- 【linux】ARM板子开启浮点和neon加速
参考 1. ARM平台NEON指令的编译和优化; 2. 交叉编译器 arm-linux-gnueabi 和 arm-linux-gnueabihf 的区别; 3. https://blog.csdn. ...