python-文本操作和二进制储存
2024-10-09 17:22:57
0x01 open方法
r read
w write
a append
b byte
test.txt内容为
yicunyiye
wutang
读取test.txt
f = open('test.txt','r',encoding='utf-8')
text = f.read()
print(text)
读取一行
f = open('test.txt','r',encoding='utf-8')
text = f.readline()
print(text)
全部
f = open('test.txt','r',encoding='utf-8')
text = f.readlines()
print(text)
输出
['yicunyiye\n', 'wutang\n']
美化
f = open('test.txt','r',encoding='utf-8')
text = f.readlines()
for t in text:
print(t.strip().replace("\n",""))
输出
yicunyiye
wutang
f = open('test.txt','r',encoding='utf-8')
for i in range(3):
text = f.readline()
print(text.strip().replace("\n",""))
输出同上
0x02 with open as
这种就不需要f.close()了
with open('test.txt','r',encoding='utf8') as f:
print(f.readlines())
写入文件
#w 不存在就创建 存在就覆盖
with open('test_two.txt','w',encoding='utf8') as f:
f.write('yicunyiye')
追加文件
#a 不存在就创建 存在就追加
with open('test_two.txt','a',encoding='utf8') as f:
f.write('\nniubi')
最新文章
- Linux下添加apache虚拟主机
- 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(六)地图搜索模块
- Unity UI on hololens
- JavaScript中的编码函数
- iOS 蓝牙开发(二)iOS 连接外设的代码实现(转)
- python程序性能分析
- Apache Tomcat配置
- linux 常用的酷炫命令
- mysql主主复制(双主复制)配置步骤
- sql分页查询语句
- 所思所想 关于asp.net界面业务分离
- C++文件逐字节对比
- Sql语句之查询所有学生所有科目分数及总分
- Android:mimeType
- android shape总结 和控制的风格定制
- bash变量
- 解决CXF的java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
- 【转载】Linux下安装、配置、启动Apache
- spring实现listener(转)
- AIOps 平台的误解,挑战及建议(中)— AIOps常见的误解