os.path模块的作用

主要是解析文件路径的拼接,拆分、组合,跨系统文件符号的兼容等功能

 1、跨系统获取路径常用的符号

import os

print(os.sep)     # \ : 获取路径的分割符
print(os.extsep)  # . : 获取文件名与扩展名分割符
print(os.pardir)  # .. :返回上层目录
print(os.curdir)  # . : 当前目录

 2、路径的分割

import os.path

PATHS = [
    '/one/two/three',
    '/one/two/three/',
    '/',
    '.',
    '',
]

for path in PATHS:
    print('{!r:>17} : {}'.format(path, os.path.split(path)))
ospath_split.py

相关文章:

  • 2021-11-07
  • 2021-09-22
猜你喜欢
  • 2021-11-24
  • 2022-01-19
  • 2019-11-22
  • 2022-12-23
  • 2021-12-31
  • 2019-02-15
相关资源
相似解决方案