读: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. linux 常用重启

    sudo reboot 为最常用重启 Linux centos关机与重启命令详解与实战 Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(r ...

  2. Vim 文件配置

    cat ~/.vimrc syntax on set nu set encoding=utf-8 set ts=4 set fileencodings=ucs-bom,utf-8,cp936 set ...

  3. <Codis><JedisPool><DeadLock>

    Overview Background: I start a thread [call thread A below]in Spark driver to handle opening files i ...

  4. L2-002. 链表去重(数组模拟)

    L2-002. 链表去重 因为数值比较小,所以直接用数组来模拟 #include<cstdio> #include<cstring> #include<iostream& ...

  5. 大数据-06-Spark之读写Hive数据

    简介 Hive中的表是纯逻辑表,就只是表的定义等,即表的元数据.Hive本身不存储数据,它完全依赖HDFS和MapReduce.这样就可以将结构化的数据文件映射为为一张数据库表,并提供完整的SQL查询 ...

  6. Python之路PythonThread,第四篇,进程4

    python3  进程/线程4 进程间同步互斥方法: from multiprocessing import Lock 创建 进程锁对象 lock = Lock() lock.acquire()  给 ...

  7. pytorch使用tensorboardX进行网络可视化

    我们知道,对于pytorch上的搭建动态图的代码的可读性非常高,实际上对于一些比较简单的网络,比如alexnet,vgg阅读起来就能够脑补它们的网络结构,但是对于比较复杂的网络,如unet,直接从代码 ...

  8. [LeetCode&Python] Problem 427. Construct Quad Tree

    We want to use quad trees to store an N x N boolean grid. Each cell in the grid can only be true or ...

  9. Sublime 官方安装方法

    1. Crtl + ` : 进入控制台模式 2. 复制下面相应版本的代码,按Enter键运行 Sublime Text 3 import urllib.request,os,hashlib; h = ...

  10. flask表单,orm,csrf

    flask表单是flask中最基本的功能. 它是负责HTML页面中数据采集的部分,它由三部分组成:表单标签,表单域,表单按钮组成,通过表单用户输入的数据提交给服务器. flask表单封装了WTForm ...