Python 读写文本(open)
来源:互联网 发布:大型系统网络拓扑图 编辑:程序博客网 时间:2024/05/16 14:33
读写参数
读写参数组合
示例
首先在左面新建一个”abc.txt”的文件,文件的内容入如下:
I
love
CSDN
只读
只读模式(默认模式)
>>>>f=open("C:/Users/Administrator/Desktop/abc.txt","r")>>>>print(f.read())IloveCSDN>>>>f.close()
只写
写入模式
>>>>f=open("C:/Users/Administrator/Desktop/abc.txt","w")>>>>f.write("test")>>>>f.close()
输出的结果是:
test
在使用”w”模式时,python会把原来的文件给覆盖掉,形成新的文件,这里注意如果写入的文件不存在,python会自动新建一个文件。
追加
追加模式
>>>>f=open("C:/Users/Administrator/Desktop/abc.txt","a")>>>>f.write("test")>>>>f.close()
输出的结果是:
I
love
CSDNtest
二进制读写
另外我们还可以设定读取和写入的方式:
以二进制方式读取:
>>>>f=open("C:/Users/Administrator/Desktop/abc.txt","rb")>>>>print(f.read())>>>>f.close()b'I\r\nlove\r\nCSDN'
with实例
import rewith open("C:/Users/Administrator/Desktop/abc.txt","r",encoding="utf-8") as f: text=f.read()text=re.sub(r"In \[.*\]:\n","[In]:",text)text=re.sub(r"Out\[.*\]:","[Out]:",text)with open("C:/Users/Administrator/Desktop/abc.txt","w",encoding="utf-8") as f: f.write(text)
0 0
- Python 读写文本(open)
- python文本读写,with open as
- Python文件操作,open读写文件,追加文本内容
- Python文件操作,open读写文件,追加文本内容
- python的open()读写操作
- python open读写函数
- Python-open读写模式
- python-open()-文件读写
- 【python】Python open读写文件
- python 文本读写操作
- Python读写文本文档详解
- python入门笔记-文本读写
- python:pandas(7),读写文本格式数据
- Python open读写文件实现脚本
- Python open读写文件实现脚本
- Python open读写文件实现脚本
- python open文件 读写模式说明
- Python open读写文件实现脚本
- linux安装nginx
- sqoop安装及使用
- opencv学习笔记(三)-绘制基本图形及文字
- JDBC基础--4.22号学习总结
- 《图解HTTP》摘记
- Python 读写文本(open)
- React-bootstrap 中文文档
- 数据结构(2):栈和队列
- BUAA 1033 (三分枚举计算 点到线段的最短距离)
- Python日期加减运算
- HashMap的工作原理
- hdu4513_吉哥系列故事――完美队形II_Manacher的数组用法
- find函数用法详解
- Java 多线程(九)——ThreadLocal类