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. 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)

    题目例如以下:

  2. xBIM 基础08 WeXplorer 简介

    系列目录    [已更新最新开发文章,点击查看详细]  一.WeXplorer 简介 WeXplorer 是 XBIM 工具包的可视化部分,它使用预处理的 WexBIM 文件在 Web 上处理 IFC ...

  3. jqGrid冻结列

    jqgrid冻结列 冻结列:就是横向移动表格时,让某一列保持不动 做法: 1.colModel的行要加上属性: frozen:true.注意:冻结列必须从第一列开始,包括隐藏列 2.加载jqgrid后 ...

  4. 【原创】Spring连接、事务代码分析

    1.JdbcTemplate     当不使用事务时,jdbcTemplate的模板类,通过     Connection con = DataSourceUtils.getConnection(ge ...

  5. swift 20 - Nested Types

    Nested Types 只是为了方便类型的整合和使用 struct BlackjackCard { // nested Suit enumeration enum Suit: Character { ...

  6. SQL Server查询死锁,杀死进程解决死锁

    查询死锁进程和表 SELECT request_session_id AS spid , OBJECT_NAME(resource_associated_entity_id) AS 'table' F ...

  7. pycharm修改提示

  8. Linux一些简单命令

    1.安装gvim:sudo apt-get install vim-gtk vim和gvim相同,只是后者比前者多了一个界面,此界面可以用来保存.新建.查找等. 三种模式,insert(i),norm ...

  9. nodejs 守护进程运行

    有四种方法: 1.forever forver start  bin/www 2.pm2 pm2 strat bin/www 3.node自身进程保护 nohup node /bin/www  > ...

  10. collections模块-namedtuple

    namedtuple -> 命名元组 这里的命名指的是对元组中元素的命名. 通过一个例子来看 import collections Person = collections.namedtuple ...