读: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 读写的更多相关文章

  1. Python读写文件

    Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...

  2. python 读写、创建 文件

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  3. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  4. [Python]读写文件方法

    http://www.cnblogs.com/lovebread/archive/2009/12/24/1631108.html [Python]读写文件方法 http://www.cnblogs.c ...

  5. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  6. 使用Python读写csv文件的三种方法

    Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗号分隔值(Comma-Separated Values,CSV,有时也称为字符分隔值,因为分隔字符也可以不是 ...

  7. python读写word、excel、csv、json文件

    http://blog.csdn.net/pipisorry/article/details/50368044 python读写word文档 (include wps)将word文档转换成txt文档 ...

  8. python读写csv文件

    文章链接:https://www.cnblogs.com/cloud-ken/p/8432999.html Python读写csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 前言 逗 ...

  9. python 全栈开发,Day86(上传文件,上传头像,CBV,python读写Excel,虚拟环境virtualenv)

    一.上传文件 上传一个图片 使用input type="file",来上传一个文件.注意:form表单必须添加属性enctype="multipart/form-data ...

  10. Python读写docx文件

    Python读写word文档有现成的库可以处理.我这里采用 python-docx.可以用pip install python-docx安装一下. 这里说一句,ppt和excel也有类似的库哦,而且是 ...

随机推荐

  1. ajax返回数据定义为全局变量

    var result;   //定义全局变量    $(document).ready(function(){          $.ajax({                   type:'PO ...

  2. 虚拟机中扩展linux系统存储空间

    reference: https://blog.csdn.net/greenapple_shan/article/details/52799631 https://blog.csdn.net/lyd1 ...

  3. mybatis学习(五)----实现关联表查询

    一.一对一的表查询 查询班级表中班级号为1的对应的记录(包括教师的具体信息) 1.首先建立数据表 数据表class和techear,class表中只有一个外键techear_id,sql脚本如下: C ...

  4. 多War包合并,jetty测试

    模块间的相互依赖引用配置在pom.xml中加入要依赖的模块即可<dependency>    <groupId>com.exayong</groupId>    & ...

  5. EF Code First MySql 主从表设计的一些需要注意的内容

    假如有下面两张表 public class Main { public int Id{get;set;} public string Name{get;set}; public virtual ICo ...

  6. Sass 混合宏、继承、占位符 详解

    混合宏-声明混合宏如果你的整个网站中有几处小样式类似,比如颜色,字体等,在 Sass 可以使用变量来统一处理,那么这种选择还是不错的.但当你的样式变得越来越复杂,需要重复使用大段的样式时,使用变量就无 ...

  7. Oracle审计策略例子

    首先确保Oracle初始化参数audit_trail值为DB或OS,通过“show parameter audit_trail:”查看. 1 语句审计 audit table by test by a ...

  8. python如何进行内存管理的

    python引用了一个内存池(memory pool)机制,即pymalloc机制(malloc:n,分配内存),用于管理对小块的申请和释放.

  9. mail命令入门及进阶

    mail是linux shell中的邮件工具,与crontab配合使用,可以实现定期发送邮件.本文主要介绍mail工具使用方法及注意事项. 1.mail命令一般用法: mail –s "邮件 ...

  10. win7快捷方式图标修复

    test.bat ///////////////////////////////////////////////////////////////////////// rem 关闭Windows外壳程序 ...