# -*- coding: utf-8 -*-

# 测试文件名为:
# text.txt
# 测试文件内容为:
# abcdefg
# 每次操作后将文件复原 # r
# 以只读方式打开文件,文件不可写
# 要打开的文件不存在时会报错
# 文件的指针将会放在文件的开头
# 这是默认模式
# # file = open('test.txt', 'r')
# # FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
# file = open('text.txt', 'r')
# print(file.read())
# # abcdefg
# file.write('aaa')
# # io.UnsupportedOperation: not writable
# file.close() # rb
# 以二进制格式打开一个文件用于只读,文件不可写
# 要打开的文件不存在时会报错
# 文件指针将会放在文件的开头
# 这是默认模式
# # file = open('test.txt', 'rb')
# # FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
# file = open('text.txt','rb')
# print(file.read())
# b'abcdefg'
# # file.write(b'aaa')
# # io.UnsupportedOperation: not writable
# file.close() # r+
# 打开一个文件用于读写,写入内容为str
# 文件指针将会放在文件的开头
# 重新写入的内容从头开始替换
# file = open('text.txt', 'r+')
# file.write('aaa')
# file.close()
# file = open('text.txt','r')
# print(file.read())
# # 'abcdefg'
# file.close() # rb+
# 以二进制格式打开一个文件用于读写,写入内容为bytes
# 文件指针将会放在文件的开头
# 重新写入的内容从头开始替换
# file = open('text.txt','rb+')
# # file.write('aaa')
# # TypeError: a bytes-like object is required, not 'str'
# file.write(b'aaa')
# file.close()
# file = open('text.txt','rb')
# print(file.read())
# # b'aaadefg'
# file.close() # w
# 打开一个文件只用于写入,写入内容为str
# 文件不可读
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'w')
# 创建一个空文件
# file = open('text.txt', 'w')
# file.write('gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close() # wb
# 以二进制格式打开一个文件只用于写入,写入内容为bytes
# 文件不可读
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'wb')
# 创建一个空文件
# file = open('text.txt', 'wb')
# file.write(b'gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close() # w+
# 打开一个文件用于读写,写入内容为str
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'w+')
# 创建一个空文件
# file = open('text.txt', 'w+')
# file.write('gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close() # wb+
# 以二进制格式打开一个文件用于读写,写入内容为bytes
# 如果该文件已存在则将其覆盖
# 如果该文件不存在,创建新文件
# file = open('text.txt', 'wb+')
# file.write(b'gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close() # a
# 打开一个文件用于追加(只写),写入内容为str
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件进行写入
# file = open('test.txt', 'a')
# 创建一个空文件
# file = open('text.txt', 'a')
# file.write('aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close() # ab
# 以二进制格式打开一个文件用于追加(只写),写入内容为bytes
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件进行写入
# file = open('test.txt', 'ab')
# 创建一个空文件
# file = open('text.txt', 'ab')
# file.write(b'aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close() # a+
# 打开一个文件用于追加(读写),写入内容为str
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件用于读写
# file = open('test.txt', 'a+')
# 创建一个空文件
# file = open('text.txt', 'a+')
# file.write('aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close() # ab+
# 以二进制格式打开一个文件用于追加(读写),写入内容为bytes
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件用于读写
# file = open('text.txt', 'ab+')
# file.write(b'aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close()

参考python open 关于读、写、追加的总结

Python open 读和写的更多相关文章

  1. python文件读和写

    fileHandle = open ( 'G:/qqfile/1.txt','w' )fileHandle.write('abcd')#写文件 地址要用反斜杠fileHandle.close() fi ...

  2. Python|读、写Excel文件(三种模块三种方式)

    python读写excel的方式有很多,不同的模块在读写的讲法上稍有区别: 用xlrd和xlwt进行excel读写: 用openpyxl进行excel读写: 用pandas进行excel读写: imp ...

  3. Python 文件读和写

  4. python之 文件读与写

    python 进行文件读写的函数是open 或filefile_handler = open(filename,,mode) 模式 描述r 以读方式打开文件,可读取文件信息.w 以写方式打开文件,可向 ...

  5. Python open()函数文件打开、读、写操作详解

    一.Python open()函数文件打开操作 打开文件会用到open函数,标准的python打开文件语法如下:open(name[,mode[,buffering]])open函数的文件名是必须的, ...

  6. python读、写、修改、追写excel文件

    三个工具包 python操作excel的三个工具包如下 xlrd: 对excel进行读相关操作 xlwt: 对excel进行写相关操作 xlutils: 对excel读写操作的整合 注意,只能操作.x ...

  7. python文件处理-读、写

    Python中文件处理的操作包括读.写.修改,今天我们一起来先学习下读和写操作. 一.文件的读操作 例一: #文件读操作 f = open(file="first_blog.txt" ...

  8. markdown简易快速的编辑格式(易读易写)

    实现简单快速书写,格式指定简便.易读易写 讲解http://wowubuntu.com/markdown/ 简单使用的讲解:http://www.ituring.com.cn/article/23 代 ...

  9. @property和@x.setter和@x.deleter表示可读可写可删除

    @property可以将python定义的函数“当做”属性访问,从而提供更加友好访问方式,但是有时候setter/deleter也是需要的.1>只有@property表示只读.2>同时有@ ...

随机推荐

  1. jqgrid表头上面再加一行---二级表头

    实现效果如图,代码如下: $( "#table_list_1" ).jqGrid( 'setGroupHeaders' , { useColSpanStyle : true , / ...

  2. openssl rsa/pkey

    openssl系列文章:http://www.cnblogs.com/f-ck-need-u/p/7048359.html openssl rsa和openssl pkey分别是RSA密钥的处理工具和 ...

  3. SpringBoot之使用Scheduled做定时任务

    定时任务有好多开源框架比如Quartz,@Scheduled是Spring的一个定时任务注解,通过注解配置就能够轻量级的定时任务,简单方便. 一.@Scheduled注解介绍 这里先贴上@Schedu ...

  4. 关于 RESTFUL API 安全认证方式的一些总结

    常用认证方式 在之前的文章REST API 安全设计指南与使用 AngularJS & NodeJS 实现基于 token 的认证应用两篇文章中,[译]web权限验证方法说明中也详细介绍,一般 ...

  5. Java并发编程:synchronized、Lock、ReentrantLock以及ReadWriteLock的那些事儿

    目录 前言 synchronized用法 修饰方法 修饰实例方法 修饰静态方法 同步代码块 引出Lock Lock用法 子类:ReentrantLock 读写分离锁:ReadWriteLock Loc ...

  6. 解决Linux服务器tomact-8.0启动慢的问题

    环境信息: CentOS release 6.8 tomcat-8.0 JDK1.8 一.启动tomcat #sh /root/tomcat-8.0/bin/startup.sh #tailf /ro ...

  7. Python十讲 - 第一讲:从零开始学Python

    之后慢慢添加... Python语言的背景知识

  8. Immuable详解以及在React中的实战

    转载自:https://zhuanlan.zhihu.com/p/20295971, 今天看到这篇文章后情不自禁的转载过来了,我的天老爷,我看到后直接菊花一紧,这写的太好了,直接写进我心坎里了,我必须 ...

  9. 【读书笔记】iOS-Objective-C编程

    Objective-C中的类可以继承自任何一个顶级类,需要注意的是,虽然NSObject是最常见的顶级类,但是它并不是唯一的顶级类,例如,NSProxy就是和NSObject一样的顶级类,所以你不能说 ...

  10. Stackoverflow 珠玑:用于分组的 LINQ 扩展方法

    从 stackoverflow.com 上抄来的,将 IEnumerable 中的元素进行切分的方法,无动态内存分配,地球上最快的实现: public static class LinqExtensi ...