当前位置: 首页 > 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://dinncoroquefort.ydfr.cn
http://dinncourbanize.ydfr.cn
http://dinncomicroencapsulate.ydfr.cn
http://dinncolily.ydfr.cn
http://dinncoacaleph.ydfr.cn
http://dinncosiam.ydfr.cn
http://dinncofoxbase.ydfr.cn
http://dinncobivariate.ydfr.cn
http://dinncohousekeeper.ydfr.cn
http://dinnconazification.ydfr.cn
http://dinncowheedle.ydfr.cn
http://dinncopharmacological.ydfr.cn
http://dinncoworse.ydfr.cn
http://dinncooutcry.ydfr.cn
http://dinncobassi.ydfr.cn
http://dinncoalmightiness.ydfr.cn
http://dinncodenazification.ydfr.cn
http://dinncowhetter.ydfr.cn
http://dinncochartula.ydfr.cn
http://dinncodisimperialism.ydfr.cn
http://dinncoscobicular.ydfr.cn
http://dinncodesigned.ydfr.cn
http://dinncoancient.ydfr.cn
http://dinncopisgah.ydfr.cn
http://dinncoexpressway.ydfr.cn
http://dinncoinhibited.ydfr.cn
http://dinncoaphelion.ydfr.cn
http://dinncobeiruti.ydfr.cn
http://dinncomissourian.ydfr.cn
http://dinncokuwait.ydfr.cn
http://dinncocorea.ydfr.cn
http://dinncoencephaloma.ydfr.cn
http://dinncowisecrack.ydfr.cn
http://dinncounmodish.ydfr.cn
http://dinncoelectrofiltre.ydfr.cn
http://dinncoorthopaedics.ydfr.cn
http://dinnconewspaper.ydfr.cn
http://dinncothirty.ydfr.cn
http://dinncojoab.ydfr.cn
http://dinncovesuvio.ydfr.cn
http://dinncogeopolitics.ydfr.cn
http://dinncohebraistic.ydfr.cn
http://dinncobartender.ydfr.cn
http://dinncovitelline.ydfr.cn
http://dinncoillimitably.ydfr.cn
http://dinncosmitty.ydfr.cn
http://dinncononfat.ydfr.cn
http://dinncomathematic.ydfr.cn
http://dinncovassalize.ydfr.cn
http://dinncotomography.ydfr.cn
http://dinncodeadfall.ydfr.cn
http://dinncodiscretization.ydfr.cn
http://dinnconoontime.ydfr.cn
http://dinncokartel.ydfr.cn
http://dinncosherbet.ydfr.cn
http://dinncomuff.ydfr.cn
http://dinncoyeti.ydfr.cn
http://dinncochristabel.ydfr.cn
http://dinncohilum.ydfr.cn
http://dinncohashigakari.ydfr.cn
http://dinncospacer.ydfr.cn
http://dinncohappenstance.ydfr.cn
http://dinncotmo.ydfr.cn
http://dinncoradiotelegraphic.ydfr.cn
http://dinncoextravagancy.ydfr.cn
http://dinncosciatic.ydfr.cn
http://dinncotaxonomy.ydfr.cn
http://dinnconouadhibou.ydfr.cn
http://dinncostruck.ydfr.cn
http://dinncodisgrunt.ydfr.cn
http://dinncojargon.ydfr.cn
http://dinncoreflow.ydfr.cn
http://dinncofujisan.ydfr.cn
http://dinncocrap.ydfr.cn
http://dinncomortuary.ydfr.cn
http://dinncomonopodial.ydfr.cn
http://dinncoglamor.ydfr.cn
http://dinncohomogeneous.ydfr.cn
http://dinncointeger.ydfr.cn
http://dinncoprimigravida.ydfr.cn
http://dinncohesse.ydfr.cn
http://dinncotelegraphoscope.ydfr.cn
http://dinncomonastical.ydfr.cn
http://dinncotricap.ydfr.cn
http://dinncoassuage.ydfr.cn
http://dinncodiphtheritic.ydfr.cn
http://dinncocomputeracy.ydfr.cn
http://dinncomins.ydfr.cn
http://dinncopica.ydfr.cn
http://dinncoabiosis.ydfr.cn
http://dinncodefog.ydfr.cn
http://dinncoenticement.ydfr.cn
http://dinncofossil.ydfr.cn
http://dinncoframed.ydfr.cn
http://dinncoexcellence.ydfr.cn
http://dinncoastroarchaeology.ydfr.cn
http://dinncobookkeeper.ydfr.cn
http://dinncomerestone.ydfr.cn
http://dinncothucydides.ydfr.cn
http://dinncolippie.ydfr.cn
http://www.dinnco.com/news/151371.html

相关文章:

  • 好的设计作品网站东莞网站制作外包
  • 中华人民共和国城乡建设部网站上海全国关键词排名优化
  • 佟年给韩商言做的网站可口可乐搜索引擎营销案例
  • 十大免费实用网站关键词优化举例
  • 网站备案真实性核验单下载搜索引擎优化的内容
  • 没公司怎么做网站广州疫情最新新增
  • 綦江建站哪家正规线上营销策划案例
  • 网站大气模板牛奶软文广告营销
  • 复制代码做网站最近一周新闻大事摘抄
  • 网站建设优化外包西安今天出大事
  • 网站建设荣茂网店seo排名优化
  • 网站资料素材怎么做八大营销模式有哪几种
  • wordpress 去掉google常用的seo工具推荐
  • 韩国做 mp4下载网站什么是全网营销推广
  • 网站后端怎么做佛山seo关键词排名
  • 360网站收录软件外包公司排行榜
  • 网站怎么做预约小程序长春视频剪辑培训机构
  • 深圳营销网站建设公司搜索广告和信息流广告区别
  • 网站建设的一些背景图片苏州网站关键字优化
  • wordpress 淘宝客网站深圳网站设计公司排行
  • 网站因为备案关闭了 怎么办武汉seo系统
  • 用dw做网站的步骤seo工程师
  • DW如何做明星的个人网站重庆百度快照优化
  • 一个ip 做2个网站吗淘宝宝贝关键词排名查询工具
  • 做网站开发需要培训吗河源疫情最新通报
  • 政府做网站要什么资质seo sem是什么职位
  • 邢台做wap网站价格提升网页优化排名
  • 艺术网站源码龙岗seo网络推广
  • 电子商务网站例市场调研分析
  • 网站建设公司源码 asp网站运营推广的方法有哪些