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')

最新文章

  1. Linux下添加apache虚拟主机
  2. 天津政府应急系统之GIS一张图(arcgis api for flex)讲解(六)地图搜索模块
  3. Unity UI on hololens
  4. JavaScript中的编码函数
  5. iOS 蓝牙开发(二)iOS 连接外设的代码实现(转)
  6. python程序性能分析
  7. Apache Tomcat配置
  8. linux 常用的酷炫命令
  9. mysql主主复制(双主复制)配置步骤
  10. sql分页查询语句
  11. 所思所想 关于asp.net界面业务分离
  12. C++文件逐字节对比
  13. Sql语句之查询所有学生所有科目分数及总分
  14. Android:mimeType
  15. android shape总结 和控制的风格定制
  16. bash变量
  17. 解决CXF的java.io.FileNotFoundException: class path resource [META-INF/cxf/cxf-extension-soap.xml] cannot be opened because it does not exist
  18. 【转载】Linux下安装、配置、启动Apache
  19. spring实现listener(转)
  20. AIOps 平台的误解,挑战及建议(中)— AIOps常见的误解

热门文章

  1. SpringCloudAlibaba-服务网关Gateway
  2. Linux基础 Day2
  3. Mybatis进阶使用-一级缓存与二级缓存
  4. Hibernate在MySQL中查询区分大小写
  5. 对接接口时,组织参数json出现的问题
  6. 高可用集群corosync+pacemaker之crmsh使用(一)
  7. 【python】装饰器听了N次也没印象,读完这篇你就懂了
  8. 良许被百万大V安排得服服帖帖,还跟美女小姐姐合影了……
  9. Centos7.6系统下docker的安装
  10. servlet web项目连接数据库报错