[Python] Working with file
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的更多相关文章
- Python中的file和open简述
help(file) help(open) 老规矩先看一下内置的帮助文档怎么描述file和open,毕竟官方文档是最直接最准确的描述. Help on class file in module __b ...
- 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 ...
- 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 ...
- Sublime Text 3 调用cmd运行c、java、python、batch file
一.调用cmd运行c(首先复制MinGW到C盘根目录,并添加环境变量) Tools --> Build System --> New Build System 删除所有内容 复制如下代码进 ...
- python 输入输出,file, os模块
Python 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout ...
- 调用python脚本报错/usr/bin/env: python : No such file or directory
一.调用python脚本报错 /usr/bin/env: python: No such file or directory 二.解决方法 原因是在windows上编写的脚本,使用dos2unix对脚 ...
- /usr/bin/env python no such file or directory: dos格式导致的!
最近修改了几个python文件,发现在linux上只能用python file来执行,直接./file提示错误"no such file or directory",而脚本是用&q ...
- Python 打开文件(File Open)
版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...
- 【python】类file文件处理
[flush方法] 通常由于缓冲,write不将数据写入文件,而是写入内存的缓冲区,需要使用flush写入文件,并清空缓冲区 文件的flush方法的作用是强制清空缓存写入文件.默认每行flush一下? ...
- python : No such file or directory
windows上写的python脚本,在linux上执行时报: No such file or directory 解决方法一# sed -i 's#\r##' mysqlchk.py 解决方法二脚本 ...
随机推荐
- nj07---npm
一.如何使用包管理器 Node.js包管理器,即npm是Node.js官方提供的包管理工具,它已经成了Node.js包的标准发布平台,用于Node.js包的发布.传播.依赖控制.(可以下载上面的包也可 ...
- zzulioj--1841--so easy!麻麻再也不用担心我的数学了!(数学水题)
1841: so easy!麻麻再也不用担心我的数学了! Time Limit: 1 Sec Memory Limit: 128 MB Submit: 27 Solved: 15 SubmitSt ...
- react --- 路由传参的几种方式
1.params 优势 : 刷新地址栏,参数依然存在缺点:只能传字符串,并且,如果传的值太多的话,url会变得长而丑陋. 2.query 优势:传参优雅,传递参数可传对象:缺点:刷新地址栏,参数丢失 ...
- zookeeper启动闪退
编辑zkServer.cmd在它的尾行加上 pause 就可以将闪退停住来观察闪退的原因. 遇到Exception in thread "main" java.lang. ...
- 细说ReactiveCocoa的冷信号与热信号(一)
热信号:事件触发: 冷信号:订阅出发: 从本质上来说,是信号的存在和产生,是静态信号和动态信号的区别. 背景 ReactiveCocoa(简称RAC)是最初由GitHub团队开发的一套基于Cocoa的 ...
- Linux常用命令及其英文全称
alias:给命令起别名 awk = "Aho Weiberger and Kernighan" ,三个作者的姓的第一个字母 bash:GNU Bourne-Again She ...
- 参考《深度学习之PyTorch实战计算机视觉》PDF
计算机视觉.自然语言处理和语音识别是目前深度学习领域很热门的三大应用方向. 计算机视觉学习,推荐阅读<深度学习之PyTorch实战计算机视觉>.学到人工智能的基础概念及Python 编程技 ...
- scrapy xpath选择器多级选择错误
在学习scrapy中用xpath提取网页内容时,有时要先提取出一整个行标签内容,再从行标签里寻找目标内容.出现一个错误. 错误代码: def parse(self, response): sel = ...
- Python 批处理文本文件、进行查找
去年换了一部手机,老手机终于光荣退休了,但是里面的便签里还存有很多文字记录,这个手机还不能备份到云,只能将每个便签保留为一个个的文本文件,我想要把所有的文本文件归到一个文本文件中,手动操作太麻烦了,刚 ...
- POI0109 POD (最短路)
POI0109 POD (最短路) 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! 现在让我们来对一个交通运输图进行研究,这 ...