#   只读模式
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. Jmeter(二十五)常见问题(转载)

    转载自 http://www.cnblogs.com/yangxia-test 收集工作中JMeter遇到的各种问题   1.  JMeter的工作原理是什么? 向服务器提交请求:从服务器取回请求返回 ...

  2. Spring AOP学习笔记

      Spring提供了一站式解决方案:          1) Spring Core  spring的核心功能: IOC容器, 解决对象创建及依赖关系          2) Spring Web ...

  3. IDEA错误:Cannot start compilation: the output path is not specified for module "Test". Specify the out

    错误是发生在从github上checkout自己的项目时.因为没有将配置文件一起上传,所以在运行Java程序时有了这个报错: Cannot start compilation: the output ...

  4. Redis备份及回收策略

    Redis备份(持久化) Redis备份存在两种方式: 1.一种是"RDB".是快照(snapshotting),它是备份当前瞬间Redis在内存中的数据记录; 2.另一种是&qu ...

  5. 在timer的时候突然改变影片简介,先前的不暂停

    import flash.display.MovieClip; import flash.utils.Timer; import flash.events.TimerEvent; var hinder ...

  6. UVa 122 Trees on the level(二叉树层序遍历)

    Trees are fundamental in many branches of computer science. Current state-of-the art parallel comput ...

  7. selenium学习一

    chrome版本和chromedriver的对应关系 chromedriver版本 支持的Chrome版本 v2.40 v66-68 v2.39 v66-68 v2.38 v65-67 v2.37 v ...

  8. 再遇ibatisNet

    11年在Mr刘的带领下第一次接触ibatisnet ,当时Mr刘很详细的很讲了xml里的写法还有配置文件之类的,但是随着时间越来越久远.很多东西都开始淡忘了. 如今,再次和它相遇,依然觉得很亲切,虽然 ...

  9. 云计算之 PaaS详解

    PaaS是Platform-as-a-Service的缩写,意思是平台即服务. Paas - 概述 计算机技术 PaaS(Platform-as-a-Service:平台即服务) 全称:(Platfo ...

  10. HR 常用事务代码

    HR的键值权限查看:  oosb 删除人员 : pu01 查看人员主数据:PA30 对人员进行入职.离职.调岗等基本操作:PA40 查看HR中的公式的意思:PE04 查看HR中的工资项:PE02 创建 ...