#   只读模式
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. Java LinkedList

    LinkedList是基于双向链表实现的,先看构造方法和方法 Constructor Summary Constructors Constructor Description LinkedList() ...

  2. Android 数据库框架总结(转)

    转自 http://blog.csdn.net/da_caoyuan/article/details/61414626 一:OrmLite 简述: 优点: 1.轻量级:2.使用简单,易上手:3.封装完 ...

  3. logic:equal 标签的使用(转)

    <logic:equal name="music" property="distrubute" value="true">    ...

  4. pta6-15(双端循环队列)

    题目链接:https://pintia.cn/problem-sets/1101307589335527424/problems/1101313244863737856 题意:实现双段队列的队首出队. ...

  5. JAVA jar 参数

    -client       to select the "client" VM    -server       to select the "server" ...

  6. Jmeter 录制脚本(二)

    1)选择WorkBench,右键 Add -> Non-Test Elements -> HTTP(S) Test Script Recorder 2)在HTTP(S) Test Scri ...

  7. swift 关于didSet 和willSet赋值的注意点

    1. 初始化赋值的时候都不会走这个的方法, 需要在创建结构体后或对象后,在赋值,此时才会走这个方法

  8. [leetcode]304. Range Sum Query 2D - Immutable二维区间求和 - 不变

    Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper lef ...

  9. 46-wxpython 4 使用 grid 展示表格

    转载:https://blog.csdn.net/soslinken/article/details/79024938#%E4%BD%BF%E7%94%A8%E6%A0%B7%E4%BE%8B wxp ...

  10. solr简介、学习详细过程!(超详细~)

    solr是什么呢? 一.Solr它是一种开放源码的.基于 Lucene Java 的搜索服务器,易于加入到 Web 应用程序中. 二.Solr 提供了层面搜索(就是统计).命中醒目显示并且支持多种输出 ...