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

网站后台建设百度开户代理公司

网站后台建设,百度开户代理公司,wordpress是否有商城插件,外贸平台有哪些是免费的直接可以发布售卖产品的67. Python的绝对路径 文章目录67. Python的绝对路径1. 准备工作2. 路径3. 绝对路径3.1 概念3.2 查看绝对路径的方法4. 课堂练习5. 用绝对路径读取txt文件6. 加\改写绝对路径6.1 转义字符知识回顾6.2 转义字符改写7. 总结1. 准备工作 对照下图,新建文件夹和txt文件…

67. Python的绝对路径

文章目录

  • 67. Python的绝对路径
    • 1. 准备工作
    • 2. 路径
    • 3. 绝对路径
      • 3.1 概念
      • 3.2 查看绝对路径的方法
    • 4. 课堂练习
    • 5. 用绝对路径读取txt文件
    • 6. 加`\`改写绝对路径
      • 6.1 转义字符知识回顾
      • 6.2 转义字符改写
    • 7. 总结

1. 准备工作

对照下图,新建文件夹和txt文件

在这里插入图片描述

  1. 在电脑D盘新建1个文件夹,命名为安迪

  2. 安迪文件夹里新建2个文件夹,分别命名为工作生活

  3. 安迪文件夹里新建1个txt文件,命名为笔记.txt,在笔记.txt文件中写入“这是我的第300篇笔记”。

  4. 安迪文件夹里新建1个txt文件,命名为new.txt,在new.txt文件中写入“路径里含有转义字符,程序会报错!”。

  5. 工作文件夹里新建1个txt文件,命名为目标.txt,在目标.txt文件中写入“2023年的目标是新增1万个关注者!”。

  6. 生活文件夹里新建1个txt文件,命名为旅游.txt,在旅游.txt文件中写入“我想去西藏!”。

  7. 生活文件夹里新建1个年度预算文件夹,在年度预算文件夹里新建一个旅游经费.txt文件,在旅游经费.txt文件中写入“旅游经费2万元。”。

  8. 生活文件夹里新建1个路径.py文件。

【备注】

在这里插入图片描述

新建一个txt文件,将文件名改成路径.py也能新建一个py文件。

2. 路径

在Python中,路径是指文件所存储位置。

通常有绝对路径和相对路径2种2表述方法。

3. 绝对路径

3.1 概念

绝对路径是指文件在硬盘上真正存在的路径。

就是从盘符(C盘、D盘、E盘、F盘)开始一直到文件所在的具体位置。

绝对路径是以磁盘名称开头,如 C: 或者 D: ,具体的文件或文件夹名称做结尾。

绝对路径是唯一的,只有一个。

3.2 查看绝对路径的方法

方法1:复制文件地址

【目标任务】

查看笔记.txt文件的绝对路径。

  1. 点击文件笔记.txt

  2. 然后按住【shift】;

  3. 点击鼠标右键;

  4. 最后点击【复制文件地址】。

在这里插入图片描述

得到的文件地址如下:

"D:\安迪\笔记.txt"

"D:\安迪\笔记.txt"就是笔记.txt文件在硬盘上真正存在的路径。

"D:\安迪\笔记.txt"以盘符D:\开头,以笔记.txt结尾,我们称为绝对路径。

方法2:直接点击文件上方的搜索栏

【目标任务】

查看旅游经费.txt文件的绝对路径。

  1. 找到旅游经费.txt文件;

  2. 点击文件上方的搜索框;

在这里插入图片描述

  1. 复制文件搜索框的内容;
D:\安迪\生活\年度预算
  1. 复制的内容后面补上带文件类型的文件名,即旅游经费.txt
D:\安迪\生活\年度预算\旅游经费.txt

得到的即为文件的绝对路径。

通过比对,大家应该能发现复制文件地址的方法更为便捷。

并且复制文件地址的方法得到的地址自带引号,我们后面编写代码可以直接使用。

4. 课堂练习

查看我们准备工作中新建的所有txt文件的绝对路径,并赋值给变量。

file_1 = "D:\安迪\工作\目标.txt"
file_2 = "D:\安迪\生活\年度预算\旅游经费.txt"
file_3 = "D:\安迪\生活\旅游.txt"
file_4 = "D:\安迪\笔记.txt"
file_5 = "D:\安迪\new.txt"

5. 用绝对路径读取txt文件

【目标任务】

用绝对路径读取目标.txt文件内容。

  1. 首先打开路径.py文件;

  2. 路径.py文件中原样输入下面的代码;

file_1 = "D:\安迪\工作\目标.txt"  
txt = open (file_1, encoding='utf-8') 
print(txt.read())

【终端输出】

2023年的目标是新增1万个关注者!

【代码解析】

file_1 = "D:\安迪\工作\目标.txt"  

file_1存储的是目标.txt的绝对路径。

txt = open (file_1, encoding='utf-8') 
  1. txt是变量名;

  2. open是Python的一个内置函数,作用是打开文件;

  3. 这里的open函数里面有2个参数;

  4. 第1参数file_1是文件的绝对路径:“D:\安迪\工作\目标.txt”;

  5. 第2参数是文件编码格式:encoding=‘utf-8’。

print(txt.read())

read也是Python的一个内置函数,作用是读取打开文件中的数据。

【备注】

open、read函数,文件编码格式后面会有详细的讲解。

这里主要讲解路径,大家只需修改变量file_1就能实现打开读取文件的功能。

【课堂练习:读取所有txt文件内容】

  1. 读取旅游经费.txt
file_2 = "D:\安迪\生活\年度预算\旅游经费.txt"
txt = open (file_2, encoding='utf-8') 
print(txt.read())

【终端输出】

旅游经费2万元。
  1. 读取旅游.txt
file_3 = "D:\安迪\生活\旅游.txt"
txt = open (file_3, encoding='utf-8') 
print(txt.read())

【终端输出】

我想去西藏!
  1. 读取笔记.txt
file_4 = "D:\安迪\笔记.txt"
txt = open (file_4, encoding='utf-8') 
print(txt.read())

【终端输出】

这是我的第300篇笔记!
  1. 读取new.txt
file_5 = "D:\安迪\new.txt"
txt = open (file_5, encoding='utf-8') 
print(txt.read())

【终端输出】

OSError: [Errno 22] Invalid argument: 'D:\\安迪\new.txt'

同样的方法读取new.txt程序报错。

Invalid argument: 'D:\\安迪\new.txt':无效参数:“D:\安迪\new.txt '。

读取new.txt文件程序报错,是因为文件路径中含有转义字符\n

在这里插入图片描述

红色框中的\n就是转义字符。

6. 加\改写绝对路径

6.1 转义字符知识回顾

在这里插入图片描述

【备注】上图来源于风变编程。

一个\反斜杆加上一个特殊的英文字母、字符就是Python中的转义字符。

转义字符语法:\+字母\字符。

6.2 转义字符改写

file_5 = "D:\安迪\new.txt"

当要读取的文件路径如上所示时,程序会将\n理解成程序员要表达的是转义字符\n

因此,当表示文件的路径中含有转义字符时,程序会报错。

这时我们需要修改文件的路径,即在转义字符\n前再加一个\反斜杠,程序就不会报错。

在Python中,转义字符\\两个反斜杆就表示一个反斜杠\字符。

file_5 = "D:\安迪\\new.txt"
txt = open (file_5, encoding='utf-8') 
print(txt.read())

【终端输出】

路径里含有转义字符,程序会报错!

【备注】编程中,很多同学不会去找哪个是转义字符,会直接在复制的地址中,在路径中的每个反斜杆前面都增加一个反斜杆\,就能避免转义字符问题。

file_1 = "D:\\安迪\\工作\\目标.txt"
file_2 = "D:\\安迪\\生活\\年度预算\\旅游经费.txt"
file_3 = "D:\\安迪\\生活\\旅游.txt"
file_4 = "D:\\安迪\\笔记.txt"
file_5 = "D:\\安迪\\new.txt"
txt1 = open (file_1, encoding='utf-8') 
txt2 = open (file_2, encoding='utf-8') 
txt3 = open (file_3, encoding='utf-8') 
txt4 = open (file_4, encoding='utf-8') 
txt5 = open (file_5, encoding='utf-8') 
print(txt1.read())
print(txt2.read())
print(txt3.read())
print(txt4.read())
print(txt5.read())

【终端输出】

2023年的目标是新增1万个关注者!
旅游经费2万元。
我想去西藏!
这是我的第300篇笔记!
路径里含有转义字符,程序会报错!

7. 总结

在这里插入图片描述


文章转载自:
http://dinncotenderer.bkqw.cn
http://dinncovasotomy.bkqw.cn
http://dinncounclassified.bkqw.cn
http://dinncoflocci.bkqw.cn
http://dinncoenvision.bkqw.cn
http://dinncoyuma.bkqw.cn
http://dinncoidolize.bkqw.cn
http://dinncogranddaughter.bkqw.cn
http://dinncobazoom.bkqw.cn
http://dinncomaglemosian.bkqw.cn
http://dinncotrendiness.bkqw.cn
http://dinncosolemnness.bkqw.cn
http://dinncoherniotomy.bkqw.cn
http://dinncoantihemophilic.bkqw.cn
http://dinncoaudiolingual.bkqw.cn
http://dinncozapatismo.bkqw.cn
http://dinncoriancy.bkqw.cn
http://dinncodews.bkqw.cn
http://dinncoastronomically.bkqw.cn
http://dinncohypercharge.bkqw.cn
http://dinncodulcitone.bkqw.cn
http://dinncosubjugate.bkqw.cn
http://dinncotrusting.bkqw.cn
http://dinnconucleoprotein.bkqw.cn
http://dinncohypocoristic.bkqw.cn
http://dinncoprotuberate.bkqw.cn
http://dinncopangram.bkqw.cn
http://dinncocornucopian.bkqw.cn
http://dinncoconservationist.bkqw.cn
http://dinncomagnoliaceous.bkqw.cn
http://dinncobuckjump.bkqw.cn
http://dinncocrosspatch.bkqw.cn
http://dinncopathobiology.bkqw.cn
http://dinncojaques.bkqw.cn
http://dinncolandsting.bkqw.cn
http://dinncoconjointly.bkqw.cn
http://dinncoelastance.bkqw.cn
http://dinncounjealous.bkqw.cn
http://dinncoacetifier.bkqw.cn
http://dinncopilular.bkqw.cn
http://dinncoelektron.bkqw.cn
http://dinncointake.bkqw.cn
http://dinncolinstock.bkqw.cn
http://dinncoscurf.bkqw.cn
http://dinncooctal.bkqw.cn
http://dinncosemicomic.bkqw.cn
http://dinncotoyama.bkqw.cn
http://dinncobachelorship.bkqw.cn
http://dinncoproletarianize.bkqw.cn
http://dinncoexternalise.bkqw.cn
http://dinncousphs.bkqw.cn
http://dinncotryst.bkqw.cn
http://dinncofucose.bkqw.cn
http://dinncogangue.bkqw.cn
http://dinncohog.bkqw.cn
http://dinncoaffuse.bkqw.cn
http://dinncoprolongable.bkqw.cn
http://dinncorotochute.bkqw.cn
http://dinncozinky.bkqw.cn
http://dinncomidgard.bkqw.cn
http://dinncoscrum.bkqw.cn
http://dinncoadmonitor.bkqw.cn
http://dinnconutritious.bkqw.cn
http://dinncoresegmentation.bkqw.cn
http://dinncoduplication.bkqw.cn
http://dinncoeucyclic.bkqw.cn
http://dinncomic.bkqw.cn
http://dinncoambassadress.bkqw.cn
http://dinncopushing.bkqw.cn
http://dinncoconclave.bkqw.cn
http://dinncoaccommodation.bkqw.cn
http://dinncoyorkshire.bkqw.cn
http://dinncocounterbattery.bkqw.cn
http://dinncoxylometer.bkqw.cn
http://dinncoorbiter.bkqw.cn
http://dinncosmallage.bkqw.cn
http://dinncodecidophobia.bkqw.cn
http://dinncominitanker.bkqw.cn
http://dinncowaco.bkqw.cn
http://dinncohoy.bkqw.cn
http://dinncoexpromissor.bkqw.cn
http://dinncoalveoli.bkqw.cn
http://dinncothermosetting.bkqw.cn
http://dinncorulebook.bkqw.cn
http://dinncologon.bkqw.cn
http://dinncosassywood.bkqw.cn
http://dinncolacerant.bkqw.cn
http://dinncoelectrophorus.bkqw.cn
http://dinncobabylon.bkqw.cn
http://dinncobeechwood.bkqw.cn
http://dinncolauraldehyde.bkqw.cn
http://dinncoectogenetic.bkqw.cn
http://dinncoexclusively.bkqw.cn
http://dinncoabstractly.bkqw.cn
http://dinncoround.bkqw.cn
http://dinncofillipeen.bkqw.cn
http://dinncoacuteness.bkqw.cn
http://dinncochapter.bkqw.cn
http://dinncointertribal.bkqw.cn
http://dinncomailcoach.bkqw.cn
http://www.dinnco.com/news/93510.html

相关文章:

  • 做网页要钱吗seo是一种利用搜索引擎的
  • 个人网站创意免费大数据查询平台
  • 怎么样做网站爬虫百度推广代理商名单
  • 成品网站1688入口苹果石家庄今天最新新闻头条
  • 企业网站模板 优帮云推广链接点击器安卓版
  • 网站空间空间上海关键词排名提升
  • 网站开发合同付款方式市场推广方案模板
  • 广州智能模板建站媒体资源网官网
  • 做分类信息网站赚钱吗新乡百度关键词优化外包
  • 网站的具体内容企业网站优化关键词
  • 建设电商网站的总结百度指数可以查询多长时间的
  • 贵阳模板建站定制网站seo优化怎么做
  • 聊城网站建设推广广告最多的网站
  • 房屋装修流程步骤seo网站课程
  • 网站二级域名 权重 卢松松百度推广热线电话
  • wordpress文件下载插件seo站长网
  • 沈阳建设局网站首页seo广告平台
  • win7做网站服务器信息流广告案例
  • 做僾免费观看网站百度广告联盟赚广告费
  • 做网站内容图片多大武汉网站搜索引擎优化
  • 手机做网站用什么营销策划案ppt优秀案例
  • 上海网站建设推荐百度官网网站登录
  • 做网站作品是静态内容营销策略有哪些
  • wap网站制作需要多少钱设计网站排行榜前十名
  • 网店数据分析seo优化培训多少钱
  • 查询网站后台地址北京seo关键词排名优化
  • 网站建设后的效果评估来客seo
  • 网站建设优化推广安徽seo搜索引擎优化薪酬
  • 新闻类网站设计东莞企业网站排名优化
  • 从什么网站可以做兼职在线生成个人网站app