2.22python基础(3) 文件操作
2018-02-23 00:23
537 查看
python的文件操作用得越来越多了,常常还是想不起来需要搜索。写博客记录一下,也帮助记忆。主要是读写。
打开使用try, 关闭finally,捕捉异常except as;或者使用简单的方法with as,可以合并前两个语句,但是无法捕捉异常。
open(), close(),或者 with open() as
open(文件路径, 操作),下面操作内容转自http://www.cnblogs.com/feeland/p/4477535.html
file一些方法:file.read file.write filr.tell()告诉位置 file.readline file writelines
str.replace()
‘Hello Joshua’.replace(‘Hello’, ‘lalala’)
‘lalala Joshua’
一个例子:
打开使用try, 关闭finally,捕捉异常except as;或者使用简单的方法with as,可以合并前两个语句,但是无法捕捉异常。
open(), close(),或者 with open() as
open(文件路径, 操作),下面操作内容转自http://www.cnblogs.com/feeland/p/4477535.html
file一些方法:file.read file.write filr.tell()告诉位置 file.readline file writelines
str.replace()
‘Hello Joshua’.replace(‘Hello’, ‘lalala’)
‘lalala Joshua’
一个例子:
try: file = open(path, "w") file.write("Hello Joshua") except errors.DfpReportError as e: print('Failed to generate report. Error was: %s' % e) finally: file.close()
相关文章推荐
- python基础3之文件操作、字符编码解码、函数介绍
- Day3 Python基础学习——文件操作、函数
- Python学习之路-基础知识2-文件操作
- Python文件基础操作(入门1)
- python基础(10)——文件操作,os模块
- python函数基础 与文件操作
- Python学习入门基础教程(learning Python)--5.6 Python读文件操作高级
- python文件的基础操作
- 【python基础知识】文件操作中的读写模式讲解
- python基础入门详解(文件输入/输出 内建类型 字典操作使用方法)
- python基础教程-文件操作
- Python基础之文件操作
- Python(五)文件操作基础代码示例
- Python学习:IO基础(2):对文件目录的操作
- Python基础7:文件操作
- Python基础学习----文件操作
- python基础(函数、局部变量、全局变量、文件操作)
- python基础-文件操作
- Python文件基础操作(入门1)
- 【Python基础】1.1.2 多文件合并操作