[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 解决方法二脚本 ...
随机推荐
- 2014 百度之星 1003 题解 Xor Sum
Xor Sum Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包括了N个正整数,随后 Prometheu ...
- bzoj1009: [HNOI2008]GT考试(kmp+矩阵乘法)
1009: [HNOI2008]GT考试 题目:传送门 题解: 看这第一眼是不是瞬间想起组合数学??? 没错...这样想你就GG了! 其实这是一道稍有隐藏的矩阵乘法,好题! 首先我们可以简化一下题意: ...
- android常用控件的使用方法
引言 xml很强大 TextView <TextView android:id="@+id/text_view" android:layout_width="mat ...
- 21.hash_map(已被废弃不再使用 被unordered_map代替)
#include <string> //老版本的unordered_map(已经废弃不再使用) #include <hash_map> #include <iostrea ...
- Pycharm在创建py文件时,如何自动添加默认文件头注释?
PyCharm是一款很好用的编写Python工程的IDE,用PyCharm创建一个Python文件或者向工程添加一个.py文件时,为了更好的使所编写的代码在各操作环境更好的运行,我们往往需要在.py文 ...
- Golang 在 Mac、Linux、Windows 下交叉编译
Golang 支持在一个平台下生成另一个平台可执行程序的交叉编译功能. Mac下编译Linux, Windows平台的64位可执行程序: CGO_ENABLED= GOOS=linux GOARCH= ...
- C# 使用指针将不同值类型赋值到字节数组中
C#指针操作字节数组 Demo(以添加short类型的值为例): //bytes:目标字节数组; offset:目标在字节数组的位置; value:添加的类型值public static unsafe ...
- caffe(6) Blob,Layer,Net 以及对应配置文件的编写
深度网络(net)是一个组合模型,它由许多相互连接的层(layers)组合而成.Caffe就是组建深度网络的这样一种工具,它按照一定的策略,一层一层的搭建出自己的模型.它将所有的信息数据定义为blob ...
- ELK安装笔记
1.jdk安装 2.logstash安装使用 #命令方式[root@ELK ELK]# tar xf logstash-5.3.2.tar.gz [root@ELK logstash-5.3.2]# ...
- 简单的 centos7&rhel7 系统初始化脚本
#!/bin/bash #描述: 基于RHEL7¢os7的初始化配置 #读取用户输入的ip read -p "输入你当前Linux的IP地址:" LAST #截取网 ...