A text file is a sequence of characters stored on a permanent medium like a hard drive, flash memory, or CD-ROM. To read a file, you can use open to create a file object:

Mode ‘r’ means that file is open for reading. The file object provides several methods for reading data, including readline.

The file object keeps track of where it is in the file, so if you invoke readline again, it picks up from where it left off. You can also use a file object in a for loop.

To write a file, you have to create a file object with mode ‘w’ as a second parameter. If the file already exists, opening it in write mode clears out the old data and starts fresh. If the file doesn’t exist, a new one is created. The write method puts data into the file. Again, the file object keeps track of where it is, so if you call write again, it add the new data to the end. When you done writing, you have to close the file.

The write function returns the length of the string, including the ‘\n’ at the end of the string.

from Thinking in Python

Reading and writing的更多相关文章

  1. Reading and Writing CSV Files in C#

    Introduction A common requirement is to have applications share data with other programs. Although t ...

  2. Reading and writing RData files

    前面添加个lapply()或者dplyr::llply()就能读取,储存多个文件了.http://bluemountaincapital.github.io/FSharpRProvider/readi ...

  3. Reading or Writing to Another Processes Memory in C# z

    http://www.jarloo.com/reading-and-writing-to-memory/ Declarations [Flags] public enum ProcessAccessF ...

  4. Apache POI – Reading and Writing Excel file in Java

    来源于:https://www.mkyong.com/java/apache-poi-reading-and-writing-excel-file-in-java/ In this article, ...

  5. PostgreSQL Reading Ad Writing Files、Execution System Instructions Vul

    catalog . postgresql简介 . 文件读取/写入 . 命令执行 . 影响范围 . 恶意代码分析 . 缓解方案 1. postgresql简介 PostgreSQL 是一个自由的对象-关 ...

  6. Analysis about different methods for reading and writing file in Java language

    referee:Java Programming Tutorial Advanced Input & Output (I/O) JDK 1.4+ introduced the so-calle ...

  7. Git Learning - By reading ProGit

    Today I begin to learn to use Git. I learn from Pro Git. And I recommend it which is an excellent bo ...

  8. Writing the first draft of your science paper — some dos and don’ts

    Writing the first draft of your science paper — some dos and don’ts 如何起草一篇科学论文?经验丰富的Angel Borja教授告诉你 ...

  9. Reading Code Is Hard

    注: 以下内容引自: https://blogs.msdn.microsoft.com/ericlippert/2004/06/14/reading-code-is-hard/ Reading Cod ...

随机推荐

  1. 【LeetCode-面试算法经典-Java实现】【168-Excel Sheet Column Title(Excell列标题)】

    [168-Excel Sheet Column Title(Excell列标题)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a positive in ...

  2. event内存泄漏

    C#内存泄漏--event内存泄漏 内存泄漏是指:当一块内存被分配后,被丢弃,没有任何实例指针指向这块内存, 并且这块内存不会被GC视为垃圾进行回收.这块内存会一直存在,直到程序退出.C#是托管型代码 ...

  3. POJ 3299 模拟

    水题,但是WA了一屏--- swap的时候忘了把读入的数字也swap了---------..[尴尬] // by SiriusRen #include <cmath> #include & ...

  4. POJ 1852 Ants O(n)

    题目: 思路:蚂蚁相碰和不相碰的情况是一样的,相当于交换位置继续走. 代码: #include <iostream> #include <cstdio> #include &l ...

  5. ZBrush中常用3D笔触效果

    3D笔触共有6种绘制方式,分别为Dots(点).Drag Rect(拖拉矩形).Freehand(徒手绘制).Color Spray(彩色喷溅).Spray(喷溅)和Drag Dot(拖拽斑点). 1 ...

  6. ccs元素分类 gcelaor

    ccs元素的分类与特点 内联元素特点: 1.和其他元素都在一行上: 2.元素的高度.宽度及顶部和底部边距不可设置: 3.元素的宽度就是它包含的文字或图片的宽度,不可改变. inline-block 元 ...

  7. [agc008d]kth-k

    题意: 给你一个长度为N的整数序列X,构造一个整数序列a满足: 1.a的长度为$N^2$,且1~N中每个数字恰好出现N次: 2.数字i在a中第i次出现的位置为$X_i$: 如果不能构造输出“No”,否 ...

  8. SP687 REPEATS - Repeats(后缀数组)

    一个初步的想法是我们枚举重复子串的长度\(L\).然后跑一遍SA.然后我们枚举一个点\(i\),令他的对应点为\(i+L\),然后求出这两个点的LCP和LCS的长度答案就是这个点的答案就是\((len ...

  9. 关于JWT(Json Web Token)的思考及使用心得

    什么是JWT? JWT(Json Web Token)是一个开放的数据交换验证标准rfc7519(php 后端实现JWT认证方法一般用来做轻量级的API鉴权.由于许多API接口设计是遵循无状态的(比如 ...

  10. Object-C,NSURL,统一资源定位器

    今天晚上最后一个例子,写完休息娱乐一会. URL,统一资源定位器,可以定位网络上的一个资源. 没啥难的,还是对象.方法.API.和Java等语言没有啥区别. 不亲自一点点写一遍,印象不深,今后进一步深 ...