当前位置: 首页 > news >正文

python基础语法 007 文件操作-2文件支持模式文件的内置函数

 1.3 文件支持的模式

模式含义
'r'open a file for reading(default)
'w'open a file for writing,creates a new file if it does not exist or truncates the file if it exists 
'x'

open a file foe exclusive creation. if the file already exists, the operation fails.独创模式,已经存在文件还写?

错误信息:fileexistserror:file wxists

'a'open for appending to the end of the file without truncating it ,creates a new file if it dose not exist
't'open in text mode (default)
'b'open in binary mode 
'+'open a file for updating (reading and writing)

A、python中的open函数没有’rw‘ 这个参数,如果需要又读又写,可以使用 'r+'  or 'w+' 来代替,否则会报下列错误

file = open('new1_file', 'rw',encoding='utf-8')
file.write("url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n")
file.write("url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n")
#读取数据
date = file.read()
file.close()"""
ValueError: must have exactly one of create/read/write/append mode
"""""" 改为:
file = open('new1_file', 'r+',encoding='utf-8')
"""

1.4 文件内置函数

1.4.1 readlines:

指:按行读取一个文本文件中的内容,并将每一行存储为一个字符串元素,最终返回一个包含所有元素的列表

file = open('new1_file', 'r+',encoding='utf-8')
file.write("url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n")
file.write("url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n")
data = file.readlines()
print(data)
file.close()"""结果
['url:/futureloan/mvc/api/member/register@mobile:18866668888@pwd:123456\n', 'url:/futureloan/mvc/api/member/recharge@mobile:18866668888@amount:1000\n']"""
f = open("demo.txt",'w+',encoding='utf-8')
f.write('new line\n')
f.write('new2 line\n')
f.close()"""
readlines() 读取每一行,会存放到列表当中,每一行的内容就是列表的一个元素
read() 得到的是一整个字符串
"""
f = open("demo.txt",'r',encoding='utf-8')
data = f.readlines()
print(data)"""结果
['new line\n', 'new2 line\n']
"""

每个元素的末尾会存在\n, 怎么去除?


"""
每个元素的末尾会存在\n#分行打印时会把\n打印,
"""
#方法一
for line in data:print(line)"""结果
new linenew2 line"""#方法二 去除\n
#strip 可以去除空格和换行
for line in data:print(line.strip())"""结果
new line
new2 line
"""#方法三 去除\n
for line, value  in enumerate(data):if line == len(data) -1:#最后一行print(value[:])else:#其他行print(value[:-1])"""结果
new line
new2 line
"""f.close()

1.4.2 tell  / seek

指控制文件光标的操作 

seek(1,2) :1表示光标移动的偏移量, 2表示相对的位置:0表示开始,1表示当前光标,2表示末尾

以字节来移动

1.4.3 with

防止忘记关闭文件

"""
with 语句可以节省关闭文件的操作
"""
f = open("demo.txt")
f.read()
f.close()  # 该步骤容易遗忘#打开文件,用f去接受
#上下文表达式
with open("demo.txt" ) as f:f.read()#防止这样的书写
# f = open("demo.txt")
# f.read()
# f = open("demo.txt", 'w')
# f.write('hellp')
# f = open("demo.txt")
# f.read()

1.5 例子:

1.5.1 将文件中的内容读取

#第一步打开文件
f = open("new_file.txt",encoding='utf-8')
data = f.read()
print(data)#换行符分割 '\n'
new_data = data.split('\n')
print(new_data)

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 数据库基础与安装MYSQL数据库
  • 解决云服务器CPU占用率接近100%问题
  • 二叉树基础及实现(一)
  • Java 写一个可以持续发送消息的socket服务端
  • c++初阶篇(三):内联函数及auto关键字
  • 代理模式详解
  • 畅玩游戏新选择 :游戏本 Windows10 64位 专业版!
  • oracle备份和恢复exp/imp-----从全库备份中恢复用户库解题思路
  • ubantu22.04安装OceanBase 数据库
  • 数据结构2—顺序表(附源码)
  • react中的context就是vue中的provide/inject吗?
  • 全国区块链职业技能大赛第八套区块链产品需求分析与方案设计
  • Go操作Redis详解
  • 校验deb、rpm、apt、yum安装文件完整性测试
  • Web 性能入门指南-3.5 优化单页应用程序 (SPA)
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • iOS编译提示和导航提示
  • Java 多线程编程之:notify 和 wait 用法
  • JavaScript 事件——“事件类型”中“HTML5事件”的注意要点
  • java取消线程实例
  • js写一个简单的选项卡
  • LintCode 31. partitionArray 数组划分
  • MD5加密原理解析及OC版原理实现
  • miniui datagrid 的客户端分页解决方案 - CS结合
  • Node + FFmpeg 实现Canvas动画导出视频
  • Redux系列x:源码分析
  • spring security oauth2 password授权模式
  • SwizzleMethod 黑魔法
  • 构造函数(constructor)与原型链(prototype)关系
  • 极限编程 (Extreme Programming) - 发布计划 (Release Planning)
  • 警报:线上事故之CountDownLatch的威力
  • 来,膜拜下android roadmap,强大的执行力
  • 在weex里面使用chart图表
  • 自定义函数
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • #Z2294. 打印树的直径
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (C#)一个最简单的链表类
  • (k8s中)docker netty OOM问题记录
  • (多级缓存)多级缓存
  • (二)【Jmeter】专栏实战项目靶场drupal部署
  • (二)原生js案例之数码时钟计时
  • (四)事件系统
  • (转)Android学习笔记 --- android任务栈和启动模式
  • (转)全文检索技术学习(三)——Lucene支持中文分词
  • .form文件_一篇文章学会文件上传
  • .net core MVC 通过 Filters 过滤器拦截请求及响应内容
  • .net core 微服务_.NET Core 3.0中用 Code-First 方式创建 gRPC 服务与客户端
  • .NET Framework 和 .NET Core 在默认情况下垃圾回收(GC)机制的不同(局部变量部分)
  • .net用HTML开发怎么调试,如何使用ASP.NET MVC在调试中查看控制器生成的html?
  • .php文件都打不开,打不开php文件怎么办
  • [ 2222 ]http://e.eqxiu.com/s/wJMf15Ku
  • [2021ICPC济南 L] Strange Series (Bell 数 多项式exp)
  • [AHK V2]鼠标悬停展开窗口,鼠标离开折叠窗口
  • [c#基础]值类型和引用类型的Equals,==的区别