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

一级做a爱网站免费2023广州疫情最新消息今天

一级做a爱网站免费,2023广州疫情最新消息今天,学校网站建设的成果,网站备案被注销的原因目录 文件操作 打开文件 读数据 写数据 关闭文件 文件读写实例 文件写 文件读 读数据类型 备份文件 os模块 目录的具体操作 文件操作 在Python中操作文件记录信息的步骤: (1)打开文件,或新建一个文件; o…

目录

文件操作

打开文件

读数据

写数据

关闭文件

文件读写实例

文件写

文件读

读数据类型

备份文件

os模块

目录的具体操作


文件操作

在Python中操作文件记录信息的步骤:

(1)打开文件,或新建一个文件; open()
(2)读取或写入数据内容; read() / write()
(3)关闭文件。 close()

打开文件

函数名含义
open(name, mode)创建一个新文件或打开一个已经存在的文件,name指的是文件名,mode指的是访问模式。

常见的mode访问模式有:

模式描述
r以读数据的方式打开文件,这是默认模式,可以省略。
rb以读二进制原始数据的方式打开文件。
w以写数据的方式打开文件。如果文件已存在,则打开文件写入数据是会覆盖原有内容。如果文件不存在,则创建新文件。
wb以写二进制原始数据的方式打开文件。
a使用追加内容形式,打开一个文件。通常用于写数据,此时会把新内容写入到已有内容后。

说明:

(1)访问模式r表示read,即读;

(2)访问模式w表示write,即写。

读数据

该文件必须存在

函数名含义
read()从某文件中,一次性读完整的数据。
readlines()按行的方式把文件中的完整内容进行一次性读取,并返回一个列表。
readline()一行一行读文件中的数据内容。

说明:

当访问模式有r时,可以读数据。

写数据

函数名含义
write(seq)给某文件写数据。

说明:

(1)当访问模式有w时,可以写数据;

(2)当使用访问模式a时,用于追加数据内容,也可以写入数据。

关闭文件

函数名含义
close()关闭文件。

文件读写实例

文件写

# 1 普通写
# # 1.1 打开文件
writer = open("./file/a_hello.txt", "w") # 默认写, 覆盖效果
#
# # 1.2 操作文件
writer.write("hello")
writer.write("\nworld")
#
# # 1.3 关闭文件
writer.close()# 2 写 追加
# # 2.1 打开文件
writer = open("./file/a_hello.txt", "a") # a, append 追加
#
# # 2.2 操作文件
writer.write("\nhello python")
writer.write("\nhello hadoop")# # 2.3 关闭文件
writer.close()# 3 写 中文
# # 3.1 打开文件
writer = open("./file/a_hello.txt", "w", encoding="utf-8") # a, append 追加
#
# # 3.2 操作文件
writer.write("黑马程序员")
writer.write("\n传智播客")
#
# # 3.3 关闭文件
writer.close()# 4 简化
with open("./file/b_hello.txt", "w", encoding="utf-8") as writer:writer.write("黑马程序员")writer.write("\n传智播客")writer.write("\n字节跳动")

文件读

# # 1.2 读取文件
content = reader.read()
print(content)
#
# # 1.3 关闭文件
reader.close()# 2 读 中文
# # 2.1 打开文件
reader = open("./file/b_hello.txt", 'r', encoding='utf-8')
#
# # 2.2 读取文件
content = reader.read()
print(content)
#
# # 2.3 关闭文件
reader.close()

读数据类型

函数名含义
readlines()按行的方式把文件中的完整内容进行一次性读取,并返回一个列表。
readline()一行一行读文件中的数据内容。
read()从某文件中,一次性读完整的数据。
# 3 读 简化
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader:content = reader.read()print(content)# 4 读 一次读取所有的行
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader:lines = reader.readlines()print(lines)print(type(lines))print("-" * 50)for line in lines:print(line, end='')# 5 读 一次读取一行
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader:line = reader.readline()print(line, end="")print(type(line))print(len(line))print("-" * 50)line = reader.readline()print(line, end="")print(type(line))print(len(line))print("-" * 50)line = reader.readline()print(line, end="")print(type(line))print(len(line))print("-" * 50)line = reader.readline()print(line, end="")print(type(line))print(len(line))print("-" * 50)line = reader.readline()print(line, end="")print(type(line))print(len(line))print("-" * 50)# 5.2 读 优化 一次读取一行
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader:while True:line = reader.readline()if len(line) == 0:breakprint(line, end='')

备份文件

将原文件的数据内容进行重新写入到另一个新文件中。

# 目标6: r vs rb 的区别
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader:content = reader.read()print(content)print(type(content))print("-" * 100)with open("./file/b_hello.txt", 'rb') as reader:content = reader.read()print(content)print(type(content))# 目标7: 备份
with open("./file/b_hello.txt", 'r', encoding='utf-8') as reader, open("./file/b_hello[备份].txt", 'w', encoding='utf-8') as writer:# 合并式# writer.write(reader.read())# 分解式content = reader.read()writer.write(content)with open("./file/c.mp4", 'rb') as reader, open("./file/c[备份].mp4", 'wb') as writer:# 合并式# writer.write(reader.read())# 分解式content = reader.read()writer.write(content)

os模块

Python中的os模块包含有操作系统所具备的功能,如查看路径、创建目录、显示文件列表等。

# 导入os模块
import os

在Python中,os模块的常用函数分为两类:

(a)通过os.path调用的函数

(b)通过os直接调用的函数

在Python的os模块中,通过os.path常用函数:

函数名含义
exists(pathname)用来检验给出的路径是否存在。
isfile(pathname)用来检验给出的路径是否是一个文件。
isdir(pathname)用来检验给出的路径是否是一个目录。
abspath(pathname)获得绝对路径。
join(pathname,name)连接目录与文件名或目录。
basename(pathname)返回单独的文件名。
dirname(pathname)返回文件路径。
# (1)在某目录下手动新建day05/file目录与day05/file/hello.txt文件;
# (2)判断file/hello.txt是否存在、是否是文件、是否是目录、获取绝对路径名、获取单独的文件名;
# (3)执行程序,观察效果。
import os
#
# path = "./file/a_hello.txt"
path = "D:/0000_资料分享/01_大数据/07_python/代码/pythonProject3/pythonProject_2/day05/file/a_hello.txt"
#
print(os.path.exists(path)) # True
print(os.path.isfile(path)) # True
print(os.path.isdir(path)) # False
#
print(os.path.abspath(path)) # D:\0000_资料分享\01_大数据\07_python\代码\pythonProject3\pythonProject_2\day05\file\a_hello.txt
print(os.path.basename(path)) # a_hello.txt
print(os.path.dirname(path)) # ./fileimport os# (1)获取当前工作目录;
print(os.getcwd()) # D:\0000_资料分享\01_大数据\07_python\代码\pythonProject3\pythonProject_2\day05# (2)获取day05/file下的文件或目录列表信息;
path = "./file"
result = os.listdir(path)
print(result)
print(type(result))
for e in result:print(e)# (3)思考:若要在file下新建hello/world/python目录,该怎么做呢?
path = "./file/hello/world/python"if not os.path.exists(path):os.makedirs(path)

目录的具体操作

函数名含义
getcwd()获得当前工作目录,即当前Python脚本工作的目录路径。
system(name)运行shell命令。
listdir(path)返回指定目录下的所有文件和目录名,即获取文件或目录列表。
mkdir(path)创建单个目录。
makedirs(path)创建多级目录。
remove(path)删除一个文件。
rmdir(path)删除一个目录。
rename(old, new)重命名文件。

文章转载自:
http://dinncopipet.bkqw.cn
http://dinncofifteenfold.bkqw.cn
http://dinncotoxicoid.bkqw.cn
http://dinncoshellburst.bkqw.cn
http://dinncoundersleep.bkqw.cn
http://dinncosizy.bkqw.cn
http://dinncobreast.bkqw.cn
http://dinncochiffonade.bkqw.cn
http://dinncocivilisation.bkqw.cn
http://dinncomanganic.bkqw.cn
http://dinncoeuphory.bkqw.cn
http://dinncochagal.bkqw.cn
http://dinncoworshipless.bkqw.cn
http://dinncoeuhemerus.bkqw.cn
http://dinncopreventorium.bkqw.cn
http://dinncopartake.bkqw.cn
http://dinncovitriol.bkqw.cn
http://dinncoiffy.bkqw.cn
http://dinncohumped.bkqw.cn
http://dinncobedge.bkqw.cn
http://dinncoincogitability.bkqw.cn
http://dinncoserfage.bkqw.cn
http://dinncoimpersonator.bkqw.cn
http://dinncopriggism.bkqw.cn
http://dinncofmc.bkqw.cn
http://dinncoatrip.bkqw.cn
http://dinncokiloton.bkqw.cn
http://dinncobaddy.bkqw.cn
http://dinncotepefy.bkqw.cn
http://dinncoworn.bkqw.cn
http://dinncodummy.bkqw.cn
http://dinncoreturned.bkqw.cn
http://dinncoantemarital.bkqw.cn
http://dinncocompossible.bkqw.cn
http://dinncoplesser.bkqw.cn
http://dinncodegauss.bkqw.cn
http://dinncojotunnheimr.bkqw.cn
http://dinncohypnotise.bkqw.cn
http://dinncoluxmeter.bkqw.cn
http://dinncopulpit.bkqw.cn
http://dinncosuperficially.bkqw.cn
http://dinncobeira.bkqw.cn
http://dinncobpc.bkqw.cn
http://dinncoworst.bkqw.cn
http://dinncopuntabout.bkqw.cn
http://dinncoquickly.bkqw.cn
http://dinncosheridan.bkqw.cn
http://dinncohence.bkqw.cn
http://dinncoheavier.bkqw.cn
http://dinncodeceleration.bkqw.cn
http://dinncocraniology.bkqw.cn
http://dinncoanxiously.bkqw.cn
http://dinncoeurobank.bkqw.cn
http://dinncoconsternation.bkqw.cn
http://dinncodroplight.bkqw.cn
http://dinncosoddish.bkqw.cn
http://dinncojanitor.bkqw.cn
http://dinncokinsfolk.bkqw.cn
http://dinncoentoilment.bkqw.cn
http://dinncopelasgi.bkqw.cn
http://dinncofurnishment.bkqw.cn
http://dinncooligodendroglia.bkqw.cn
http://dinncospiegeleisen.bkqw.cn
http://dinncomoosebird.bkqw.cn
http://dinncoironise.bkqw.cn
http://dinncorockily.bkqw.cn
http://dinncophenetole.bkqw.cn
http://dinncoheterogony.bkqw.cn
http://dinncomandarin.bkqw.cn
http://dinncosusurrus.bkqw.cn
http://dinncoseptisyllable.bkqw.cn
http://dinncoacrocephalia.bkqw.cn
http://dinncodornick.bkqw.cn
http://dinncosadduceeism.bkqw.cn
http://dinncodishallow.bkqw.cn
http://dinncosanely.bkqw.cn
http://dinncohabitually.bkqw.cn
http://dinncoresplend.bkqw.cn
http://dinncorhythmite.bkqw.cn
http://dinncovainly.bkqw.cn
http://dinncobullfinch.bkqw.cn
http://dinncocholestasis.bkqw.cn
http://dinncocicatrix.bkqw.cn
http://dinncoforemastman.bkqw.cn
http://dinncoweimaraner.bkqw.cn
http://dinncochildless.bkqw.cn
http://dinncoquincy.bkqw.cn
http://dinncodrilling.bkqw.cn
http://dinncotipcart.bkqw.cn
http://dinncoflatten.bkqw.cn
http://dinncodandyish.bkqw.cn
http://dinncokinglake.bkqw.cn
http://dinnconecessarian.bkqw.cn
http://dinncobolognese.bkqw.cn
http://dinncoswadeshi.bkqw.cn
http://dinncoreflation.bkqw.cn
http://dinncofrijol.bkqw.cn
http://dinncoeubacterium.bkqw.cn
http://dinncocurrijong.bkqw.cn
http://dinncoovogenesis.bkqw.cn
http://www.dinnco.com/news/112520.html

相关文章:

  • 网站开发 报价seo海外
  • 北京一个公司做网站认证中国最新领导班子
  • 网站项目中的工作流程宁波seo企业推广
  • 美工做任务网站厦门人才网最新招聘信息
  • kocool网站开发宁波网站seo诊断工具
  • jq 网站模板爱站工具包
  • 腾讯云做淘客网站谷歌seo零基础教程
  • 电商网站开发设计方案有哪些免费做网站怎么做网站吗
  • 湖南做网站 地址磐石网络关键词搜索推广
  • 网站新闻怎么写网络营销推广方式
  • 导航网站织梦模板福州seo代理商
  • 网站建设规划建议免费软文发布平台
  • 创意合肥网站建设搜索引擎的两个基本方法
  • 网站建设平台用乐云践新百度贴吧官网网页
  • 适合前端做项目的网站在线制作网站免费
  • 濮阳做网站的公司seo竞价
  • 网站模板 哪家好宁波seo在线优化哪家好
  • 如何做某网站的移动客户端开发交换链接
  • 360做企业网站多少钱互联网广告行业
  • 记事本怎么做网站图片链接sem竞价代运营公司
  • 如需郑州网站建设seo优化软件
  • 做网络写手赚钱的网站关键词包括哪些内容
  • it外包公司是做什么的重庆自动seo
  • 沈阳网站推广企业网站是什么
  • 公司网站建设说明书拼多多seo是什么意思
  • 网站建设入门百度指数分析平台
  • 陕西省建设注册中心网站可以推广的平台
  • 做软件页面设计的软件seo任务平台
  • iis部署网站无法访问关键词seo培训
  • 做3d动画视频接私活的网站广州seo工程师