读文件的正确方法及其背后机制
2017-12-06 11:46
232 查看
应该使用以下格式读文件:
with open(...) as f: for line in f: <do something with line>
其理由如下:
The [code]withstatement handles opening and closing the file, including if an exception is raised in the inner block. [/code]
The [code]for line in ftreats the file object
fas an iterable, which automatically uses buffered IO and memory management[/code]
so you don't have to worry about large files.
相关文章推荐
- 用 PHP 读取文件的正确方法
- 多线程访问文件问题中WaitForSingleObject方法的使用,及其效率!
- 嵌入式Linux文件系统及其存储机制分析
- (转)嵌入式Linux文件系统及其存储机制分析
- 嵌入式Linux文件系统及其存储机制分析
- 嵌入式Linux文件系统及其存储机制分析
- 嵌入式Linux文件系统及其存储机制分析
- Hibernate延时加载lazy机制问题及其解决方法
- 用PHP读取文件的正确方法(3)
- 用PHP读取文件的正确方法(1)
- 用 PHP 读取文件的正确方法
- PHP 读取文件的正确方法
- VC++2005 编写只有资源文件的DLL及其调用方法
- MMC卡的文件系统及其实现方法
- Hibernate延时加载lazy机制问题及其解决方法
- 用PHP读取文件的正确方法(4)
- 嵌入式Linux的文件系统及其存储机制分析
- 嵌入式Linux文件系统及其存储机制分析
- 关于文件保存到数据库及其读取的方法
- 用 PHP 读取文件的正确方法