Python接口自动化——文件基本操作
2019-06-24 16:32
204 查看
在接口自动化中,常用到的文件操作应该是在测试套件生成测试报告中。菜鸟教程中已经写得很详细了,这里我罗列几个常用操作。菜鸟教程链接:https://www.runoob.com/python3/python3-file-methods.html
open() 方法
Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。
注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。
1.逐行读取文件内容
[code]#test:filepath r+:可读可写 f=open('test','r+',encoding='utf-8') print(f.read()) f.close() 》》》 Hello world !
2.读取整行
[code]print(f.readline()) 》》》 Hello
3.以列表形式返回所有行
[code]print(f.readlines()) 》》》 ['Hello\n', 'world\n', '!']
4.写入文件,默认不换行
[code]f.write('你好'+'\n'+'世界') #\n换行符 f.seek(0) print(f.read()) 》》》 Hello world ! 你好 世界
5.自动关闭文件的形式(常用)
[code]with open('test','r',encoding='utf-8') as f: print(f.read()) 》》》 Hello world ! 你好 世界
相关文章推荐
- [python]基本文件操作
- Python写自动化之文件、目录的一些操作(新增、移动、删除)
- python文件相关的基本操作
- [Python ] python中文件的基本操作
- hdfs基本操作-python接口
- Python基础:文件的基本操作
- Python第四天(基本文件操作二)
- python基本文件操作(文件输入和输出)
- Python 操作文件的基本方法总结
- python中文件的基本操作
- python 高级 文件的基本操作 魔法方法 面向对象 概念
- python文件路径的基本操作
- Python程序中用csv模块来操作csv文件的基本使用教程
- Python文件与文件夹常见基本操作总结
- 我的Python学习之路之基本语法--参数传递、文件操作
- Python基本文件操作
- hdfs基本操作-python接口
- Python-csv文件基本操作
- Python读取txt文件数据的方法(用于接口自动化参数化数据)
- python 文件的基本操作