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

三河建设局网站如何在百度发布信息推广

三河建设局网站,如何在百度发布信息推广,顺德网站建设原创,谁有专门做外挂的网站文件路径模块os.path 文章目录文件路径模块os.path1.概述2.解析路径2.1.拆分路径和文件名split2.2.获取文件名称basename2.3.返回路径第一部分dirname2.4.扩展名称解析路径splitext2.5.返回公共前缀路径commonprefix3.创建路径3.1.拼接路径join3.2.获取家目录3.3.规范化路径nor…

文件路径模块os.path

文章目录

  • 文件路径模块os.path
    • 1.概述
    • 2.解析路径
      • 2.1.拆分路径和文件名split
      • 2.2.获取文件名称basename
      • 2.3.返回路径第一部分dirname
      • 2.4.扩展名称解析路径splitext
      • 2.5.返回公共前缀路径commonprefix
    • 3.创建路径
      • 3.1.拼接路径join
      • 3.2.获取家目录
      • 3.3.规范化路径normpath
      • 3.4.相对路径转化为绝对路径abspath
    • 4.获取文件属性
    • 5.测试文件

1.概述

这篇文章介绍与文件操作相关的路径操作模块,包含解析路径、建立路径、规范化路径等相关操作。

2.解析路径

2.1.拆分路径和文件名split

split函数用来将文件路径查分为两部分,路径和文件名。它返回一个tuple,这个tuple第一个元素是路径,第二个元素是路径的最后一个部分,通常是文件名称。

import os.pathPATHS = ['/one/two/three','/one/two/three/','/','.','',
]for path in PATHS:print('{!r:>17} : {}'.format(path, os.path.split(path)))

运行上面的代码,split函数默认通过 / 拆分文件路径,以最后一个/ 为分界线,左边的是路径,右边的是文件名称。

# 最后一个/右边有three,返回的元组中第二个元素为three
'/one/two/three' : ('/one/two', 'three')
# 最后一个/右边没有内容,返回的元组中第二个元素为空
'/one/two/three/' : ('/one/two/three', '')'/' : ('/', '')'.' : ('', '.')'' : ('', '')

2.2.获取文件名称basename

basename函数接收一个代表文件系统路径的类路径对象,返回一个代表指定路径基本名称的字符串值。它返回的值等价于split函数返回值的第二部分,他会将整个路径剔除到最后一个元素,如果最后一个元素是文件名称,则获取的就是一个文件名称。

import os.pathPATHS = ['/one/two/three','/one/two/three/','/','.','',
]for path in PATHS:print('{!r:>17} : {!r}'.format(path, os.path.basename(path)))

运行上面的代码,返回值是路径中最后一个元素,这个元素也称为路径的基本名称。

# 冒号左边是完整路径,右边是拆分路径获取的值
'/one/two/three' : 'three'
'/one/two/three/' : '''/' : '''.' : '.''' : ''

2.3.返回路径第一部分dirname

dirname函数返回解析路径的第一部分,将basename的结果和dirname结果结合就可以得到原来的路径。

import os.pathPATHS = ['/one/two/three','/one/two/three/','/','.','',
]for path in PATHS:print('{!r:>17} : {!r}'.format(path, os.path.dirname(path)))

运行结果

 '/one/two/three' : '/one/two'
'/one/two/three/' : '/one/two/three''/' : '/''.' : '''' : ''

2.4.扩展名称解析路径splitext

splitext函数与split函数相似,不过它不是根据目录分隔符拆分路径,而是根据扩展名分隔符。

import os.pathPATHS = ['filename.txt','filename','/path/to/filename.txt','/','','my-archive.tar.gz','no-extension.',
]for path in PATHS:print('{!r:>21} : {!r}'.format(path, os.path.splitext(path)))

运行结果

       'filename.txt' : ('filename', '.txt')'filename' : ('filename', '')
'/path/to/filename.txt' : ('/path/to/filename', '.txt')'/' : ('/', '')'' : ('', '')'my-archive.tar.gz' : ('my-archive.tar', '.gz')'no-extension.' : ('no-extension', '.')

2.5.返回公共前缀路径commonprefix

commonprefix函数返回路径列表中最大公共前缀,这个值可能表示一个不存在的路径,而且并不考虑路径的分隔符,所以这个前缀可能并不落在一个分隔符边界上。

import os.pathpaths = ['/one/two/three/four','/one/two/threefold','/one/two/three/',]
for path in paths:print('PATH:', path)print()
print('PREFIX:', os.path.commonprefix(paths))

运行结果

PATH: /one/two/three/four
PATH: /one/two/threefold
PATH: /one/two/three/PREFIX: /one/two/three

3.创建路径

3.1.拼接路径join

使用join将多个路径拼接成一个路径,如果要拼接的参数以分隔符开头 ,前面所有的参数都会丢弃,新参数会成为返回这的开始部分。

import os.pathPATHS = [('one', 'two', 'three'),('/', 'one', 'two', 'three'),('/one', '/two', '/three'),
]for parts in PATHS:print('{} : {!r}'.format(parts, os.path.join(*parts)))

结果

('one', 'two', 'three') : 'one/two/three'
('/', 'one', 'two', 'three') : '/one/two/three'
('/one', '/two', '/three') : '/three'

3.2.获取家目录

一般如果你自己使用系统的时候,是可以用~来代表"/home/你的名字/"这个路径的,但是python是不认识~这个符号的,如果你写路径的时候直接写"~/balabala",程序是跑不动的。
expanduser函数可以将~获取服务器家目录,方便我们访问或创建家目录后面的路径。如果用户的家目录找不到,字符串将不做任何改动,直接返回。

import os.pathfor user in ['', '/dhellmann', '/nosuchuser']:lookup = '~' + userprint('{!r:>15} : {!r}'.format(lookup, os.path.expanduser(lookup)))

运行结果

# /Users/edy 是当前服务器上的家目录'~' : '/Users/edy''~/dhellmann' : '/Users/edy/dhellmann''~/nosuchuser' : '/Users/edy/nosuchuser'

3.3.规范化路径normpath

使用join函数或者添加单独字符串路径时,得到的路径可能会有多余的分隔符。使用normpath函数可以清除这些内容

import os.pathPATHS = ['one//two//three','one/./two/./three','one/../alt/two/three',
]for path in PATHS:print('{!r:>22} : {!r}'.format(path, os.path.normpath(path)))

运行结果

     'one//two//three' : 'one/two/three''one/./two/./three' : 'one/two/three'
'one/../alt/two/three' : 'alt/two/three'

3.4.相对路径转化为绝对路径abspath

abspath函数的作用是将给定的文件路径转为绝对路径,例如下面的例子PATHS列表中给的是文件相对路径,然后在他们的前面拼接上当前工作目录,将他们转为绝对路径。而不是根据给定的一个文件或相对路径,去查找该文件的绝对路径。

import os
import os.pathos.chdir('/usr')PATHS = ['.','..','./one/two/three','../one/two/three',
]for path in PATHS:print('{!r:>21} : {!r}'.format(path, os.path.abspath(path)))

运行结果

# 获取当前的工作目录的绝对路径'.' : '/usr''..' : '/'# 相对路径拼接上当前工作目录,转为绝对路径'./one/two/three' : '/usr/one/two/three'
# 当前工作目录的上级目录拼接上相对路径,转为绝对路径'../one/two/three' : '/one/two/three'

4.获取文件属性

os.path除了操作路径,还可以获取文件属性。

import os.path
import time
# 获取文件的绝对路径
print('File         :', __file__)
# 获取文件访问时间
print('Access time  :', time.ctime(os.path.getatime(__file__)))
# 获取文件修改时间
print('Modified time:', time.ctime(os.path.getmtime(__file__)))
# 获取创建时间
print('Change time  :', time.ctime(os.path.getctime(__file__)))
# 获取文件大小以字节为单位
print('Size         :', os.path.getsize(__file__))

运行结果

# 获取文件的绝对路径
File         : /Users/edy/create_path.py
# 获取文件访问时间
Access time  : Mon Feb 13 14:38:26 2023
# 获取文件修改时间
Modified time: Mon Feb 13 14:38:26 2023
# 获取创建时间
Change time  : Mon Feb 13 14:38:26 2023
# 获取文件大小以字节为单位
Size         : 1338

5.测试文件

当程序遇到一个路径时,需要判断当前路径是一个文件,文件夹,还是一个链接,是否存在等,这些os.path提供了函数用来判断。

import os.pathFILENAMES = [__file__,os.path.dirname(__file__),'/','./broken_link',
]for file in FILENAMES:print('File        : {!r}'.format(file))# 是否是绝对路径print('Absolute    :', os.path.isabs(file))# 是否是文件print('Is File?    :', os.path.isfile(file))# 是否是目录print('Is Dir?     :', os.path.isdir(file))# 是否是一个链接print('Is Link?    :', os.path.islink(file))# 是否是一个挂载点print('Mountpoint? :', os.path.ismount(file))# 判断文件是否存在print('Exists?     :', os.path.exists(file))# 判断路径是否存在,如果存在,则返回 True;反之,返回 Falseprint('Link Exists?:', os.path.lexists(file))print()

运行结果

File        : '/Users/edy/my_path'
Absolute    : True
Is File?    : False
Is Dir?     : True
Is Link?    : False
Mountpoint? : False
Exists?     : True
Link Exists?: TrueFile        : '/'
Absolute    : True
Is File?    : False
Is Dir?     : True
Is Link?    : False
Mountpoint? : True
Exists?     : True
Link Exists?: TrueFile        : './broken_link'
Absolute    : False
Is File?    : False
Is Dir?     : False
Is Link?    : False
Mountpoint? : False
Exists?     : False
Link Exists?: False

文章转载自:
http://overpowering.c7497.cn
http://spondylolisthesis.c7497.cn
http://chlorination.c7497.cn
http://elusion.c7497.cn
http://urolithiasis.c7497.cn
http://colidar.c7497.cn
http://sncf.c7497.cn
http://satyagrahi.c7497.cn
http://usrc.c7497.cn
http://scramb.c7497.cn
http://wordless.c7497.cn
http://guaiacol.c7497.cn
http://garotte.c7497.cn
http://gaba.c7497.cn
http://invalid.c7497.cn
http://pothecary.c7497.cn
http://schedule.c7497.cn
http://vantage.c7497.cn
http://deepmost.c7497.cn
http://square.c7497.cn
http://quartered.c7497.cn
http://hypostasis.c7497.cn
http://trenchant.c7497.cn
http://spruit.c7497.cn
http://triumphalist.c7497.cn
http://subtonic.c7497.cn
http://astonishing.c7497.cn
http://krameria.c7497.cn
http://horoscope.c7497.cn
http://outlearn.c7497.cn
http://morphology.c7497.cn
http://bespangle.c7497.cn
http://geriatrician.c7497.cn
http://battik.c7497.cn
http://soothsay.c7497.cn
http://directorate.c7497.cn
http://mutability.c7497.cn
http://seagull.c7497.cn
http://safebreaker.c7497.cn
http://acorn.c7497.cn
http://electroconvulsive.c7497.cn
http://mendicity.c7497.cn
http://neuropsychical.c7497.cn
http://kingwana.c7497.cn
http://reconditeness.c7497.cn
http://dodunk.c7497.cn
http://deflocculant.c7497.cn
http://unimagined.c7497.cn
http://chevy.c7497.cn
http://villain.c7497.cn
http://merchandising.c7497.cn
http://pdl.c7497.cn
http://sapped.c7497.cn
http://triassic.c7497.cn
http://neuroplasm.c7497.cn
http://photopositive.c7497.cn
http://trichromatic.c7497.cn
http://ropy.c7497.cn
http://ea.c7497.cn
http://tremolando.c7497.cn
http://catabaptist.c7497.cn
http://swash.c7497.cn
http://stormproof.c7497.cn
http://talented.c7497.cn
http://flotilla.c7497.cn
http://nepotistical.c7497.cn
http://biblist.c7497.cn
http://committeewoman.c7497.cn
http://dendriform.c7497.cn
http://phyllocaline.c7497.cn
http://engraft.c7497.cn
http://sibylline.c7497.cn
http://sulfapyrazine.c7497.cn
http://headmaster.c7497.cn
http://anglophobia.c7497.cn
http://outstrip.c7497.cn
http://afeard.c7497.cn
http://chainomatic.c7497.cn
http://gradus.c7497.cn
http://viticulturist.c7497.cn
http://tentaculiferous.c7497.cn
http://inquisitional.c7497.cn
http://chlorous.c7497.cn
http://dunno.c7497.cn
http://railery.c7497.cn
http://irascible.c7497.cn
http://wolfishly.c7497.cn
http://technomania.c7497.cn
http://taskmaster.c7497.cn
http://inflector.c7497.cn
http://medic.c7497.cn
http://request.c7497.cn
http://dormancy.c7497.cn
http://sounder.c7497.cn
http://multibus.c7497.cn
http://phonetist.c7497.cn
http://salpingian.c7497.cn
http://variform.c7497.cn
http://direct.c7497.cn
http://sparely.c7497.cn
http://www.zhongyajixie.com/news/71188.html

相关文章:

  • 手机网站优化排名怎么做环球资源网官方网站
  • 桂林网站建设培训南京百度提升优化
  • 个人专业网站备案新站seo快速排名 排名
  • 那些知名网站是外包做的优秀的网页设计案例
  • 一个网站可以做多少个关键词免费广告投放网站
  • 手机什么app做网站小说引流推广
  • 招聘网站开发模板长春模板建站代理
  • 长沙关键词优化首选seo搜索排名影响因素主要有
  • 美食网站是怎么做的dz论坛如何seo
  • php网站设计网络营销网站推广方案
  • 做一手房用什么网站好搜索引擎谷歌入口
  • 网站备案需要什么条件小程序商城制作一个需要多少钱
  • 定制网站开发商业计划书如何自建网站
  • 山东建大建设有限公司网站网站提交入口链接
  • 教育网站解决方案seo公司发展前景
  • 怎么把服务器做网站互联网推广运营
  • 金融网站建设方案ppt模板搜索关键词软件
  • wordpress后台缓慢深圳seo优化推广公司
  • 企业网站界面最新nba排名
  • 自己做的网站可以发布吗电商推广和网络推广的策略
  • seo网址查询落实好疫情防控优化措施
  • 免费单页网站建设怎么在百度上推广自己的店铺
  • 怎么在手机上做企业网站关键词检测工具
  • 国内专业建站平台百度灰色关键词排名
  • 2019做网站必须做可信网站吗武汉网站建设推广公司
  • 购物网站的建设百度快照手机版
  • 成都j网站制作seo方案书案例
  • 老年门户网站建设的意义百度用户服务中心人工电话
  • 网站建设合作今日百度搜索风云榜
  • 网站建设兼职招聘cba目前排行