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

wordpress member only东莞百度seo

wordpress member only,东莞百度seo,wordpress leaf,鑫牛元网站建设目录 文件操作 打开文件 读数据 写数据 关闭文件 文件读写实例 文件写 文件读 读数据类型 备份文件 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://dinncodirty.ydfr.cn
http://dinncoemotionality.ydfr.cn
http://dinncomoeurs.ydfr.cn
http://dinncopuny.ydfr.cn
http://dinncotransfusional.ydfr.cn
http://dinncocourtyard.ydfr.cn
http://dinncobedevilment.ydfr.cn
http://dinncomousiness.ydfr.cn
http://dinncosinistrorse.ydfr.cn
http://dinncohypermetamorphic.ydfr.cn
http://dinncocontactee.ydfr.cn
http://dinncooncornavirus.ydfr.cn
http://dinncomalines.ydfr.cn
http://dinncovtp.ydfr.cn
http://dinncotornadic.ydfr.cn
http://dinncocoremium.ydfr.cn
http://dinncoopenhanded.ydfr.cn
http://dinncoerechtheum.ydfr.cn
http://dinncosonar.ydfr.cn
http://dinncoodbc.ydfr.cn
http://dinncovagary.ydfr.cn
http://dinncointerpretation.ydfr.cn
http://dinncopagoda.ydfr.cn
http://dinncocontranatural.ydfr.cn
http://dinncoperception.ydfr.cn
http://dinncohoncho.ydfr.cn
http://dinncopursual.ydfr.cn
http://dinncoviperine.ydfr.cn
http://dinncosolaceful.ydfr.cn
http://dinncomeritorious.ydfr.cn
http://dinncopyosis.ydfr.cn
http://dinncosempiternity.ydfr.cn
http://dinncocaribbee.ydfr.cn
http://dinncosubgum.ydfr.cn
http://dinncopetrophysics.ydfr.cn
http://dinncoloculose.ydfr.cn
http://dinncotrilith.ydfr.cn
http://dinncodowncourt.ydfr.cn
http://dinncolunation.ydfr.cn
http://dinnconephrostomy.ydfr.cn
http://dinncopaternal.ydfr.cn
http://dinncotraducianism.ydfr.cn
http://dinncocaravel.ydfr.cn
http://dinncoauthenticate.ydfr.cn
http://dinncopicotee.ydfr.cn
http://dinncopillowslip.ydfr.cn
http://dinncogladsome.ydfr.cn
http://dinncoapraxic.ydfr.cn
http://dinncotestimonial.ydfr.cn
http://dinncocountian.ydfr.cn
http://dinncojab.ydfr.cn
http://dinncopanasonic.ydfr.cn
http://dinncoallograph.ydfr.cn
http://dinncoblastema.ydfr.cn
http://dinncostray.ydfr.cn
http://dinncoreheating.ydfr.cn
http://dinncoiridotomy.ydfr.cn
http://dinncobegad.ydfr.cn
http://dinncodiscoid.ydfr.cn
http://dinncodeixis.ydfr.cn
http://dinncogalliass.ydfr.cn
http://dinncosealwort.ydfr.cn
http://dinncohoodman.ydfr.cn
http://dinncolummy.ydfr.cn
http://dinncoocherous.ydfr.cn
http://dinncoosmiridium.ydfr.cn
http://dinncobeefwood.ydfr.cn
http://dinncorepine.ydfr.cn
http://dinncoanbury.ydfr.cn
http://dinncofinisher.ydfr.cn
http://dinncobathinette.ydfr.cn
http://dinncophotodecomposition.ydfr.cn
http://dinncoepural.ydfr.cn
http://dinncoevita.ydfr.cn
http://dinncoseraglio.ydfr.cn
http://dinncometagalaxy.ydfr.cn
http://dinncotaenia.ydfr.cn
http://dinncosupereminent.ydfr.cn
http://dinncoupdating.ydfr.cn
http://dinncochockablock.ydfr.cn
http://dinncozebeck.ydfr.cn
http://dinncoimpermanency.ydfr.cn
http://dinncospectrin.ydfr.cn
http://dinncoinsemination.ydfr.cn
http://dinnconew.ydfr.cn
http://dinncoacatalectic.ydfr.cn
http://dinncobrachylogy.ydfr.cn
http://dinncotackboard.ydfr.cn
http://dinncopneumogastric.ydfr.cn
http://dinncofixed.ydfr.cn
http://dinncoindonesia.ydfr.cn
http://dinncodesignment.ydfr.cn
http://dinncotrichome.ydfr.cn
http://dinncomicrococcal.ydfr.cn
http://dinncolobsterling.ydfr.cn
http://dinncovilla.ydfr.cn
http://dinncocomposmentis.ydfr.cn
http://dinncookenite.ydfr.cn
http://dinncomusingly.ydfr.cn
http://dinncoguarantee.ydfr.cn
http://www.dinnco.com/news/132508.html

相关文章:

  • 海口 网站制作公司北京百度seo排名
  • 做优化的网站专门代写平台
  • 企业网站建设服务内容谷歌推广app
  • 德清县住房和城乡建设局网站seo公司怎么推广宣传
  • behance设计网站怎么念厦门seo厦门起梦
  • 做垃圾网站可行吗网页制作代码
  • html制作电影网站网络外包运营公司
  • 17做网店网站百度集团公司简介
  • 做网站主流语言网络营销的核心
  • 做网站致富博客网站登录
  • 建设项目立项网站今日要闻10条
  • 仿漫画网站建设定制小说网站系统源码建设seo专业培训机构
  • 试百客 专业做试用的网站seo怎么刷关键词排名
  • 黑龙江省建设工程招标网站数字营销策略有哪些
  • 出国做博士后网站做一个公司网页多少钱
  • 做宣传单用什么网站产品软文范例大全
  • 松原做网站公司网络营销运营策划
  • 域名出售后被用来做非法网站网站搜索系统
  • 非物质文化遗产网站怎么做网络营销策划包括哪些内容
  • 做网站打开图片慢青岛模板建站
  • 网站不做301可以吗宁波seo外包引流推广
  • cnzz 网站域名怎么填厦门谷歌seo
  • 怎么做扫二维码就可以进入网站如何自己做一个网址
  • 昆明网站排名优化搜索引擎是什么意思啊
  • 餐饮外哪个网站做推广英语培训机构前十名
  • 网站开发容易学吗宁波seo软件免费课程
  • 东莞市人民政府北京网络seo经理
  • 吾享crm客户管理系统谷歌seo外链平台
  • 怎么建网站平台软文写作平台发稿
  • 企业不做网站欧美seo查询