Python allows you to open a file, do operations on it, and automatically close it afterwards using with.

>>> with open('/my_path/my_file.txt','r') as f:
>>> file_data = f.read()

In the example above we open a file, perform the operations in the block below the with statement (in this case read from the file) and afterwards Python closes it for us. No need to call

f.close()

In the previous code, the call to f.read() had no arguments passed to it; it defaults to reading all the remainder of the file from its current position - the whole file. If you pass .read() an integer argument, it will read up to that number of characters, output all of them, and keep the 'window' at that position ready to read on.

>>> with open(camelot.txt) as song:
… print(song.read(2))
… print(song.read(8))
… print(song.read())
We
're the
knights of the round table
We dance whenever we're able

Conveniently, Python will loop over the lines of a file using the syntax for line in file. I can use this to create a list of lines in the file. Because each line still has its newline character attached, I remove this using .strip().

>>> camelot_lines = []
>>> with open("camelot.txt") as f:
... for line in f:
... camelot_lines.append(line.strip())
...
>>> print(camelot_lines)
["We're the knights of the round table", "We dance whenever we're able"]

[Python] Working with file的更多相关文章

  1. Python中的file和open简述

    help(file) help(open) 老规矩先看一下内置的帮助文档怎么描述file和open,毕竟官方文档是最直接最准确的描述. Help on class file in module __b ...

  2. Error : Must specify a primary resource (JAR or python or R file)

    spark-submit 报错:must specify resource 取消关注 | 1 ... 我的submit.sh内容: /bin/spark-submit \ --class abc.pa ...

  3. Python load json file with UTF-8 BOM header - Stack Overflow

    Python load json file with UTF-8 BOM header - Stack Overflow 3 down vote Since json.load(stream) use ...

  4. Sublime Text 3 调用cmd运行c、java、python、batch file

    一.调用cmd运行c(首先复制MinGW到C盘根目录,并添加环境变量) Tools --> Build System --> New Build System 删除所有内容 复制如下代码进 ...

  5. python 输入输出,file, os模块

    Python 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout ...

  6. 调用python脚本报错/usr/bin/env: python : No such file or directory

    一.调用python脚本报错 /usr/bin/env: python: No such file or directory 二.解决方法 原因是在windows上编写的脚本,使用dos2unix对脚 ...

  7. /usr/bin/env python no such file or directory: dos格式导致的!

    最近修改了几个python文件,发现在linux上只能用python file来执行,直接./file提示错误"no such file or directory",而脚本是用&q ...

  8. Python 打开文件(File Open)

    版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...

  9. 【python】类file文件处理

    [flush方法] 通常由于缓冲,write不将数据写入文件,而是写入内存的缓冲区,需要使用flush写入文件,并清空缓冲区 文件的flush方法的作用是强制清空缓存写入文件.默认每行flush一下? ...

  10. python : No such file or directory

    windows上写的python脚本,在linux上执行时报: No such file or directory 解决方法一# sed -i 's#\r##' mysqlchk.py 解决方法二脚本 ...

随机推荐

  1. [SDOI2010] 古代猪文 (快速幂+中国剩余定理+欧拉定理+卢卡斯定理) 解题报告

    题目链接:https://www.luogu.org/problemnew/show/P2480 题目背景 “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色 ...

  2. 10.bitset

    #include <iostream> //位运算,处理二进制非常方便,线性存储 #include <bitset> #include <string> using ...

  3. 桌面出现removable storage devices文件夹无法删除解决办法

    今天桌面突然出现 removable storage devices 文件夹,且没有删除选项. 解决办法:往电脑里插一下u盘文件夹就会自动消失了.

  4. Vue简单用法目录总结 以及 前端基础总结传送门:

    Vue官方网址:https://cn.vuejs.org/ Vue 第三方组件:Element:http://element-cn.eleme.io/#/zh-CN Vue 基础指令以及自定义指令:h ...

  5. java9新特性-6-多版本兼容jar包

    1.官方Feature 238: Multi-Release JAR Files 2.使用说明 当一个新版本的Java出现的时候,你的库用户要花费数年时间才会切换到这个新的版本.这就意味着库得去向后兼 ...

  6. RecyclerView让列表嵌套如此简单

    平常开发时,相信像这样的页面,大家一定是遇到过的.这里比较坑爹的地方在于呢:列表嵌套.订单列表中的每一项,都包含一个商品列表.像这种需求,大家会如何实现呢? 这里呢,说一下我自己的思路,我没有使用列表 ...

  7. c# static 常量

    1 关键字 static 修饰 类  字段 属性 方法 ,标记static的就不用创建类的实例调用了,直接通过类名点出来 2 用于变量前,表示每次重新使用该变量所在的方法,类或者自定义的类时,变量的值 ...

  8. CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法

    CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法 CentOS 7.4 中, 网卡命名方式发生改变, 可预测功能命名: 网卡简要名称组成格 ...

  9. last-child到底怎么用

    今天工作时候遇到的坑, 看来还是css基础不够扎实,特此记录一下, <div> <p>1</p> <p>2</p> <p>3&l ...

  10. 如何解决本地仓库和远程仓库的冲突(Conflict)

    Background: 我有一个github仓库管理我的代码,我将这个仓库的代码clone到我的工作电脑和私人电脑本地方便我上班和在家时都可以对我的代码进行更新. 一天,我在家修改过代码之后并未提交, ...