file types:

  • plaintext files, such as .txt .py
  • Binary files, such as .docx, .pdf, iamges, spreadsheets, and executable programs(.exe)

steps to read/write files

  1. call the open() function to return a File object
  2. Call the read() or write() method on the File object
  3. Close the file by calling the close() method on the File object

To open the file in 'reading plaintext' mode (read mode):

>>> helloFile=open('/user/kaiming/Python/hello.txt')

>>> helloFile=open('/user/kaiming/Python/hello.txt', 'r')

where 'r' stands for read mode

the call to open() returns a File object, and assigned to the variable helloFile

To get a list of string values from the file, one string for each line of text, use readline() function

Writing to files >>> open ('hello.txt', 'w') # write mode >>> open ('hello.txt', 'a') # append mode

Note:

  1. when a file is opened in read mode, Python lets you only read data from

the file; you can't write or modify it in any way.

  1. if the filename passed to open() does not exist, both

write and append mode will create a new, blank file

>>> baconFile = open('bacon.txt', 'w')  # create a blank file named 'bacon.txt'
>>> baconFile.write('Hello world!\n')
13
>>> baconFile.close()
>>> baconFile = open('bacon.txt', 'a')
>>> baconFile.write('Bacon is not a vegetable.')
25
>>> baconFile.close()
>>> baconFile = open('bacon.txt')
>>> content = baconFile.read()
>>> baconFile.close()
>>> print(content)
Hello world!
Bacon is not a vegetable.

Created: 2019-03-06 周三 06:13

Emacs 25.3.1 (Org mode 8.2.10)

Validate

reading/writing files in Python的更多相关文章

  1. Huge CSV and XML Files in Python, Error: field larger than field limit (131072)

    Huge CSV and XML Files in Python January 22, 2009. Filed under python twitter facebook pinterest lin ...

  2. Working with Excel Files in Python

    Working with Excel Files in Python from: http://www.python-excel.org/ This site contains pointers to ...

  3. Reading Csv Files with Text_io in Oracle D2k Forms

    Below is the example to read and import comma delimited csv file in oracle forms with D2k_Delimited_ ...

  4. Unsupervised Image-to-Image Translation Networks --- Reading Writing

    Unsupervised Image-to-Image Translation Networks --- Reading Writing 2017.03.03 Motivations: most ex ...

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

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

  6. Creating Excel files with Python and XlsxWriter(通过 Python和XlsxWriter来创建Excel文件(xlsx格式))

    以下所有内容翻译至: https://xlsxwriter.readthedocs.io/ #----------------------------------------------------- ...

  7. 【Selenium】【BugList4】执行pip报错:Fatal error in launcher: Unable to create process using '""D:\Program Files\Python36\python.exe"" "D:\Program Files\Python36\Scripts\pip.exe" '

    环境信息: python版本:V3.6.4 安装路径:D:\Program Files\python36 环境变量PATH:D:\Program Files\Python36;D:\Program F ...

  8. Read Large Files in Python

    I have a large file ( ~4G) to process in Python. I wonder whether it is OK to "read" such ...

  9. How to read and write multiple files in Python?

    Goal: I want to write a program for this: In a folder I have =n= number of files; first read one fil ...

随机推荐

  1. P2746 [USACO5.3]校园网Network of Schools(Tarjan)

    P2746 [USACO5.3]校园网Network of Schools 题目描述 一些学校连入一个电脑网络.那些学校已订立了协议:每个学校都会给其它的一些学校分发软件(称作“接受学校”).注意即使 ...

  2. P2600 [ZJOI2008]瞭望塔

    传送门 暴力也行,退火也行,不是很明白为啥还要用半平面交-- 总之就是把原来的所有限制看成一堆半平面 根据黄学长的博客塔肯定建在转折处最优 //minamoto #include<bits/st ...

  3. centos docker 安装mysql 8.0

    centos 版本  CentOS Linux release 7.5.1804 (Core) 内核版本: 3.10.0-862.el7.x86_64 下载最新版mysql docker pull m ...

  4. Hexo 添加Live2D看板娘

    title: Hexo 添加 Live2D看板娘 二次元什么的最喜欢了[大好きです] 准备 项目地址 live2d模型 部分模型预览 开始 首先进入Hexo博客根目录安装live2d插件 $ npm ...

  5. 【题解】TES-Intelligence Test

    [题解]\(TES-Intelligence\) \(Test\) 逼自己每天一道模拟题 传送:\(TES-Intelligence\) \(Test\) \([POI2010]\) \([P3500 ...

  6. Java compiler level does not match the version of the installed Java project facet问题处理

    从SVN上下载应用后在Problems面板中提示以下错误信息: Java compiler level does not match the version of the installed Java ...

  7. jQuery学习笔记(1)-初探

    一.jQuery是什么 1.jQuery是一套JavaScript脚本库,而不是框架:就好比"System是程序集"是类库,而"ASP.NET MVC"是框架: ...

  8. Java虚拟机内存详解

    概述 Java虚拟机会自动管理内存,不容易出现内存泄漏和内存溢出问题.Java虚拟机会在执行过程中将管理的内存分为若干个不同的数据区域. 运行时数据区域 在jdk1.8之前的版本与1.8版本略有不同, ...

  9. C#和Java在语法上的差异(原创,持续更新中)

    1.switch  C#一直支持String类型 Java直到1.7才支持 2.C#里String有Length属性 Java里是Length方法 3.C#中修饰class的sealed效果与Java ...

  10. Scala-基础-运算符

    import junit.framework.TestCase /** * 运算符 */ class Demo3 extends TestCase { def test_+ { var x = 10; ...