#   只读模式
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. 【MongoDB】关于无法连接mongo的问题

    今天使用MongoDB的时候发现直接输入mongo提示连接失败 首先想到的可能是服务还没启动 当我随便打开一个cmd输入net start MongoDB 提示:net start mongodb 发 ...

  2. pta l2-8(最长对称字串)

    题目链接:https://pintia.cn/problem-sets/994805046380707840/problems/994805067704549376 题意:求给定字符串的最长回文串的长 ...

  3. iOS 10 之后权限设置

    iOS 10 之后权限设置 麦克风权限:Privacy - Microphone Usage Description 是否允许此App使用你的麦克风? 相机权限: Privacy - Camera U ...

  4. swift - 正则表达式

    import UIKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoa ...

  5. 27-java String 之间比较的幺蛾子

    仔细看啊, 他有双引号啊!!!!

  6. git ----(2)

    Git使用40个16进制字符的SHA-1 Hash来唯一标识对象 Git的四种基本对象类型,组成了Git更高级的数据结构: blobs:      每个blob代表一个(版本的)文件,blob只包含文 ...

  7. WEB框架Django之ORM操作

    一 ORM的简介 MVC或者MVC框架中包括的一个重要部分就是ORM,它实现了数据模型与数据库的解耦. 即数据模型的设计不需要依赖于特定的数据库,通过简单的配置可以轻松更换数据库,这可以大大减少开发人 ...

  8. centos7下docker1.12.5学习笔记

    一.Docker简介 Docker是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,也可以实现虚拟化.容器是完全使用沙箱机制,相 ...

  9. stark组件开发之URL分发和默认Handler

    为register 函数添加一个,prev参数,默认None ,用于可以让用户自己指定前缀. def register(self, model_class, handler_class=None, p ...

  10. django分页的东西, 不详细, 但是也足够了。

    视图函数中的代码 from django.shortcuts import render, HttpResponse, redirect import json from django.core.pa ...