python文件读写
来源:互联网 发布:算法统宗 pdf 编辑:程序博客网 时间:2024/04/29 22:57
1. 最基本的读文件方法:
File: readline-example-1.py
file = open("sample.txt")while 1: line = file.readline() if not line: break pass # do something
一行一行得从文件读数据,显然比较慢;不过很省内存。
2. 用fileinput模块
# File: readline-example-2.pyimport fileinputfor line in fileinput.input("sample.txt"): pass
3. 带缓存的文件读取
# File: readline-example-3.pyfile = open("sample.txt")while 1: lines = file.readlines(100000) if not lines: break for line in lines: pass # do something
在Python 2.2以后,我们可以直接对一个file对象使用for循环读每行数据:
# File: readline-example-5.pyfile = open("sample.txt")for line in file: pass # do something
而在Python 2.1里,你只能用xreadlines迭代器来实现:
# File: readline-example-4.pyfile = open("sample.txt")for line in file.xreadlines(): pass # do something
0 0
- python--python读写文件
- Python读写Unicode文件
- python Ini文件读写
- Python读写文件
- Python读写文件
- Python读写ini文件
- Python读写文件
- Python读写文件
- Python文件的读写
- python读写文件
- python文件读写
- [Python]读写文件方法
- python 文件读写操作
- Python文件读写操作
- Python读写文件
- python之读写文件
- python读写文件
- python文件的读写
- 安卓群英传 _个人笔记
- Spring 框架参考文档(三)之Spring AOP APIs
- HDU 1496 Equations 哈希
- 《如何阅读一本书》2
- 正则表达式在iOS中的运用
- python文件读写
- bs4中TypeError问题
- 请求转发和重定向的区别
- Leetcode Find All Duplicates in an Array
- 考研总结
- Js apply方法详解
- ActivityManagerService的启动过程
- oracle左外连接、右外连接、完全外连接以及(+)号用法
- 【转】使用codepush进行ReactNative热部署