#   只读模式
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. django创建一个简单的web站点

    一.新建project 使用Pycharm,File->New Project…,选择Django,给project命名 (project不能用test命名)   新建的project目录如下: ...

  2. 【Scheme】元循环求值

    #lang scheme (require rnrs/base-6) (require rnrs/mutable-pairs-6) (define (eval exp env) (cond ((sel ...

  3. 彻底弄懂css中单位px和em,rem的区别

    PX:PX实际上就是像素,用PX设置字体大小时,比较稳定和精确.但是这种方法存在一个问题,当用户在浏览器中浏览我们制作的Web页面时,如果改变了浏览器的缩放,这时会使用我们的Web页面布局被打破.这样 ...

  4. ES3之变量提升 ( hoisting )

    JavaScript引擎在预编译时,会将声明(函数声明.变量声明)自动提升至函数或全局代码的顶部.但是赋值不会提升. Because variable declarations (and declar ...

  5. [剑指Offer]35-复杂链表的复制

    链接 https://www.nowcoder.com/practice/f836b2c43afc4b35ad6adc41ec941dba?tpId=13&tqId=11178&tPa ...

  6. Delphi异步编程:匿名线程与匿名方法

    异步编程,是项目中非常有用的而且常用的一种方法,大多以线程实现. 而Delphi传统方法使用线程略为烦琐,好在其后续版本中,提供一些方法,简化一些操作. 几个概念: 匿名线程:TAnonymousTh ...

  7. 【校招面试 之 网络】第2题 TCP的可靠传输、流量控制、滑动窗口

    1.可靠传输 (1)三次握手 TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接: (1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B,并进入SYN_S ...

  8. cgi,fast-cgi,php-cgi,php-fpm转载详解

    转载自:https://segmentfault.com/q/1010000000256516 首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编 ...

  9. httpclient的简单使用

    1.通过get请求后台,注意tomcat的编码设置成utf-8;    <Connector connectionTimeout="20000" port="808 ...

  10. ----转载----【前端工具】Chrome 扩展程序的开发与发布 -- 手把手教你开发扩展程序

    关于 chrome 扩展的文章,很久之前也写过一篇.清除页面广告?身为前端,自己做一款简易的chrome扩展吧. 本篇文章重在分享一些制作扩展的过程中比较重要的知识及难点. 什么是 chrome 扩展 ...