当前位置:首页 > python > 正文内容

python 写入文件

zhangsir3年前 (2022-08-29)python280

一、读写txt文件


1、打开txt文件

Note=open('x.txt',mode='w',encoding='utf-8')

函数=open(x.扩展名,mode=模式)


模式种类:


w       只能操作写入(如果而文件中有数据,再次写入内容,会把原来的覆盖掉)
r        只能读取
a       向文件追加
w+     可读可写
r+      可读可写
a+     可读可追加
wb+   写入数据


2、向文件中写入数据


第一种写入方式:


write 写入

Note.write('hello word 你好 \n') #\n 换行符


3、关闭文件

Note.close()

二、读取txt文件

content=Note.read()


zhangsir版权a1防采集https://mianka.xyz

扫描二维码推送至手机访问。

版权声明:本文由zhangsir or zhangmaam发布,如需转载请注明出处。

本文链接:https://www.mianka.xyz/post/23.html

标签: python
分享给朋友:

“python 写入文件” 的相关文章

scrapy创建项目与运行

打开命令行:1. 创建爬虫的项目   scrapy startproject 项目的名字                 注意:项目的名字不允许使用数字开头  也不能包含中文2....

在Linux操作系统上使用selenium库

安装selenium模块命令:pip3 install selenium安装chrome命令:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64....

python之seleniumwire获取network(网络)信息

python之seleniumwire获取请求头参数import time from seleniumwire import webdriver # 创建Chrome驱动程序的新实例 driver = webdriver...

pip安装三方库 国内的一些镜像站点推荐

pip 国内的一些镜像站点推荐镜像套路:使用cmd;输入命令pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple 包名 即可开始安装。清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http...

权大师商标查询api

''' data:2022-10-15 autor:zhangsir 权大师商标查询api ''' import requests import json import time import h...

python selenium find_element_by_xpath 方法已经被弃用的解决办法

背景:在使用最新3.10.4Python版本时候,用selenium进行xpath定位元素,编译器提示:DeprecationWarning:find_element_by_xpath is deprecated. Please use find_element(by=By.XPATH, value...