#   只读模式
with open ( "file.txt" ,'r' ) as f:
        for line in f.readlines():
                print ( line )
# 读写,可以写,内容在文件最开头
with open ( "file.txt" ,'r+' ) as f:
        #for line in f.readlines():
        f.write('r+ ...') # w : 普通的写模式,如文件不存在,则建立 # w+ : 如果文件不存在,则建立
with open ( "file.write2.txt" , 'w+' ) as f:
        for line in f.read():
            print (line)
        f.write('w+ ...2 ') # rb : read binary 读取一些非文本形式,二进制形式文件用到 # wb : write binary 写一些PDF,二进制形式的文件需要 英文参考如下:
The argument mode points to a string beginning with one of the following

sequences (Additional characters may follow these sequences.):


``r''   Open text file for reading.  The stream is positioned at the

         beginning of the file


``r+''  Open for reading and writing.  The stream is positioned at the

         beginning of the file.


``w''   Truncate file to zero length or create text file for writing.

         The stream is positioned at the beginning of the file.


``w+''  Open for reading and writing.  The file is created if it does not

         exist, otherwise it is truncated.  The stream is positioned at

         the beginning of the file.


``a''   Open for writing.  The file is created if it does not exist.  The

         stream is positioned at the end of the file.  Subsequent writes

         to the file will always end up at the then current end of file,

         irrespective of any intervening fseek(3) or similar


``a+''  Open for reading and writing.  The file is created if it does not

         exist.  The stream is positioned at the end of the file.  Subse-

         quent writes to the file will always end up at the then current

         end of file, irrespective of any intervening fseek(3) or similar.

python r r+ w w+ rb 文件打开模式的区别的更多相关文章

  1. C语言中文件打开模式(r/w/a/r+/w+/a+/rb/wb/ab/rb+/wb+/ab+)浅析

    C语言文件打开模式浅析 在C语言的文件操作语法中,打开文件文件有以下12种模式,如下图: 打开模式  只可以读   只可以写  读写兼备 文本模式 r w a r+ w+ a+ 二进制模式 rb wb ...

  2. 关于open函数文件打开模式的有意思的一个现象

    老猿前阵子学习了文件IO,最近正在回顾及进行各种验证和总结,老猿在对文件进行打开后的返回值检查属性时,发现文件打开返回的文件对象的读写模式与打开文件的模式并不完全相同,如下案例: fp1 = open ...

  3. C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)

    1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...

  4. python的I/O编程:文件打开、操作文件和目录、序列化操作

    1 文件读写 1.1 打开文件: open(r'D:\text.txt') 1.2 文件模式 值 功能描述 'r' 读模式 'w' 写模式 'a' 追加模式 'b' 二进制模式 '+' 读写模式 1. ...

  5. python文件打开模式&time&python第三方库

    r:以只读方式打开文件.文件的指针将会放在文件的开头.这是默认模式. w:打开一个文件只用于写入.如果该文件已存在则将其覆盖.如果该文件不存在,创建新文件. a:打开一个文件用于追加.如果该文件已存在 ...

  6. Python——文件打开模式辨析

    版权声明:本文系原创,转载请注明出处及链接. Python中,open()函数打开文件时打开模式如r.r+ .w+.w.a.a+有何不同 r 只能读 r+ 可读可写,不会创建不存在的文件.如果直接写文 ...

  7. C\C++中 fopen中文件打开方式的区别:

    在C语言中,大家常用到fopen打开文件,准备进行写操作,再用fwrite把数据写入文件,最后用fclose关闭文件. 如以下C代码:   #include <stdio.h> char ...

  8. c语言文件打开模式

    (转载) 在C语言的文件操作语法中,打开文件文件有以下12种模式,如下图: 打开模式  只可以读   只可以写  读写兼备 文本模式 r w a r+ w+ a+ 二进制模式 rb wb ab  rb ...

  9. linux文件打开模式

     文件打开 int open(const char *pathname, int flags, mode_t mode); 普通方式(Canonical mode) flags中没有设置O_SYN ...

随机推荐

  1. Numpy:ndarray数据类型和运算

    Numpy的ndarray:一种多维数组对象 N维数组对象,该对象是一个快速而灵活的大数据集容器,nadarry是一个通用的同构数据多维容器,也就是说,其中的所有元素必须是相同类型的.每个数组都有一个 ...

  2. 织梦 列表页 list标签 按照自已设置的方式排序

    一.可以按照权重排序 降序排序 desc 1.添加的文章默认权重是自动加1,所以只要把想置顶的文章权重设置很高,如10000 2.{dede:list pagesize='12′ orderby='w ...

  3. 安装python3 及virtual与virtualenvwrapper

    安装python3 下载python源码包 网址:https://www.python.org/downloads/release/python-362/ 下载地址:https://www.pytho ...

  4. PHP面向对象之类的自动加载

    类的自动加载 含义: 当某行代码需要一个类的时候,php的内部机制可以做到“自动加载该类文件”,以满足该行需要一个类的这种需求. 什么时候需要一个类? 1,new一个对象的时候: 2,使用一个类的静态 ...

  5. exercise 1-6

    [买菜] package Practice06; import java.io.File; import java.io.FileNotFoundException; import java.util ...

  6. spring源码分析(一)

    一.首先分析AliasRegistry接口. 1.Alias别名,Registry注册表,AliasRegistry别名注册表接口. 2.共有四个方法,注册别名,判断是否别名,获取别名数组,移除别名. ...

  7. git--(3)分支 合并

    git branch test //新建分支 git branch //列出分支 git branch -r //列出远程分支 git branch -m | -M oldbranch newbran ...

  8. node.js中对同步,异步,阻塞与非阻塞的理解

    我们都知道javascript是单线程的,node.js是一个基于Chrome V8 引擎的 javascript 运行时环境,注意 node.js 不是一门语言,别搞错了. javascript为什 ...

  9. go语言使用go-sciter创建桌面应用(五) 加载元素资源

    有些时候我们需要动态的给某个UI元素加载内容或数据. demo6.go代码如下: package main; import ( "github.com/sciter-sdk/go-scite ...

  10. 如何获取google Api Key

    1.使用keytool申请获取MD5值 1.1 找到你的debug.keystore Windows Vista/7: C:\Users\<username>\.android\debug ...