「python」文件的读写
来源:互联网 发布:耳机音量放大器软件 编辑:程序博客网 时间:2024/06/11 00:56
文件的读写
写数据(write)
使用write()可以完成向文件写入数据
demo:
f = open('test.txt', 'w')f.write('hello world, i am here!')f.close()
注意:
- 如果文件不存在那么创建,如果存在那么就先清空,然后写入数据
读数据(read)
使用read(num)可以从文件中读取数据,num表示要从文件中读取的数据的长度(单位是字节),如果没有传入num,那么就表示读取文件中所有的数据
demo:
f = open('test.txt', 'r')content = f.read(5)print(content)print("-"*30)content = f.read()print(content)f.close()
注意:
- 如果open是打开一个文件,那么可以不用谢打开的模式,即只写
open('test.txt')
- 如果使用读了多次,那么后面读取的数据是从上次读完后的位置开始的
读数据(readlines)
就像read没有参数时一样,readlines可以按照行的方式把整个文件中的内容进行一次性读取,并且返回的是一个列表,其中每一行的数据为一个元素
#coding=utf-8f = open('test.txt', 'r')content = f.readlines()print(type(content))i=1for temp in content: print("%d:%s"%(i, temp)) i+=1f.close()
读数据(readline)
#coding=utf-8f = open('test.txt', 'r')content = f.readline()print("1:%s"%content)content = f.readline()print("2:%s"%content)f.close()
阅读全文
0 0
- 「python」文件的读写
- 「python」文件的随机读写
- Python文件的读写
- python文件的读写
- Python 文件的读写
- python文件的读写
- Python文件的读写
- Python的读写文件
- 【Python】文件的读写
- python文件的读写
- python正确的文件读写
- Python 文件的读写操作
- 学习 Python 文件的读写
- Python读写文件的方法
- Python读写文件的方式
- python 文件的读写操作
- python的文件读写笔记
- 「Python」python读写csv文件并增加行列
- 关于Java设计模式
- 微信小程序多行省略
- 富士康和运营商都坐不住了,这是LoRa物联网络要火的节奏
- 【IIS】IIS部署MVC5 网站 403.14 (1)
- 阿里笔试题-猴子摘桃子
- 「python」文件的读写
- 阿里quick_bi和biee报表工具的对比
- Error:Jack is required to support java 8 language features. Either enable Jack or remove...
- 关于存放visual studio 运行后的垃圾问题.
- 在opencv3.2中打开摄像头
- jks文件转换成ctr,key文件
- 【解析】全球物联网平台的发展现状
- 有了Xshell,连接远程主机不是难事!
- wordCount相关RDD的深度解析