首页 > 编程语言 >python打开文件时的mode选择

python打开文件时的mode选择

时间:2023-06-04 20:22:29浏览次数:33  
标签:文件 python mode file txt open example Opens

常用:

追加:a

覆盖:w

Mode Description Example
r Read mode. Opens the file for reading (default mode). If the file doesn’t exist, an error will be raised. file = open('example.txt', 'r')
w Write mode. Opens the file for writing. If the file exists, it will be truncated. If the file doesn’t exist, it will be created. file = open('example.txt', 'w')
a Append mode. Opens the file for writing, but appends new data to the end of the file instead of overwriting existing data. If the file doesn’t exist, it will be created. file = open('example.txt', 'a')
x Exclusive creation mode. Opens the file for writing, but only if it doesn’t already exist. If the file exists, an error will be raised. file = open('example.txt', 'x')
b Binary mode. Opens the file in binary mode instead of text mode. file = open('example.txt', 'rb')
t Text mode (default). Opens the file in text mode instead of binary mode. file = open('example.txt', 'rt')
+ Update mode. Opens the file for both reading and writing. file = open('example.txt', 'r+')

 

标签:文件,python,mode,file,txt,open,example,Opens
From: https://www.cnblogs.com/freecheng/p/17456223.html

相关文章

  • 实验5 文件应用编程
    实验任务6源代码1withopen('data6.csv','r',encoding='gbk')asf:2data=list(f.readlines())3data=[i.strip('\n')foriindata]45importdecimal6decimal.getcontext().rounding='ROUND_HALF_UP'......
  • python tkinter scale 滑动选择刻度条
    tkinter.Scale(d_f,from_=0,to=20,tickinterval=5,orient="horizontal")1.参数汇总归纳总结Scale组件中一些常用的参数以及用法。 2.方法汇总coords(value=None)获得当前滑块的位置对应Scale组件左上角的相对坐标如果设置value参数,则返回当滑块所在该位置......
  • Effective Modern C++(四)再探移动语义与完美转发
    移动语义移动语义是c++11最为重要的特性之一,但这不代表着我们可以在任何时候都无脑地使用它。在以下几个情况下,移动语义并没有什么用处。没有移动操作:要移动的对象没有提供移动操作,所以移动的写法也会变成复制操作。比如对于STL库中的array容器而言,他的元素都直接存储在了......
  • 基于2.8版本redis配置文件中文解释
        在Redis中直接启动redis-server服务时,采用的是默认的配置文件。采用redis-server xxx.conf这样的方式可以按照指定的配置文件来运行Redis服务。下面是Redis2.8.9的配置文件各项的中文解释。1#daemonizeno默认情况下,redis不是在后台运行的,如果需要在后台运......
  • 基于Selenium库的python爬虫脚本,爬取painterest上的图片
    基于Selenium库的python爬虫脚本,爬取painterest上的图片问题背景Pinterest是一个社交媒体平台,它提供了一个虚拟的个人兴趣画板,让用户可以收集和分享他们喜欢的图片、视频和链接。上面有许多优秀的图片供浏览和下载,但是一个个点图片下载非常麻烦。于是想要用Python语言写一个自......
  • Effective Modern C++(三)引用折叠
    template<typenameT>voidfunc(T&&param);对于一个通用引用,只有当实参被用来实例化通用引用形参时,才会推导形参T。编码机制是简单的。当左值实参被传入时,T被推导为左值引用。当右值被传入时,T被推导为非引用。WidgetwidgetFactory();//返回右值的函数Widgetw;......
  • Python爬虫入门六之Cookie的使用
     大家好哈,上一节我们研究了一下爬虫的异常处理问题,那么接下来我们一起来看一下Cookie的使用。为什么要使用Cookie呢?Cookie,指某些网站为了辨别用户身份、进行session跟踪而储存在用户本地终端上的数据(通常经过加密)比如说有些网站需要登录后才能访问某个页面,在登录之前,你想抓取某个......
  • 使用 Nginx Upload Module 实现上传文件功能
    普通网站在实现文件上传功能的时候,一般是使用Python,Java等后端程序实现,比较麻烦。Nginx有一个Upload模块,可以非常简单的实现文件上传功能。此模块的原理是先把用户上传的文件保存到临时文件,然后在交由后台页面处理,并且把文件的原名,上传后的名称,文件类型,文件大小set到页面。下面和大......
  • python中生产者和消费者理论
    1.模型理论生产者消费者他是一个概念,(由于生产者消费者模型并不局限于某一类技术,因此,有多种实现方式)所以,代码很简单,所以这里首先要弄懂理论。 1.1 生产者消费者模型模型指的是一种解决问题的套路。 1.2生产者消费者模型中包含两类重要角色一类叫生产者,另一类叫消费者......
  • pnpm:无法加载文件 C:\Users\Five\AppData\Roaming\npm\pnpm.ps1 ,因为在此系统
    前言重装完了电脑系统,运行pnpm无法加载文件,pnpm-V也不行解决方案用管理员方式启动powershell输入命令:set-ExecutionPolicyRemoteSigned然后再输入:Y重启代码编辑器 ......