[雪峰磁针石博客]python库介绍-os.path: 平台独立的文件名操作

273 阅读3分钟
原文链接: click.aliyun.com

Title: python库介绍-os.path: 平台独立的文件名操作
Date: 2018-03-25 19:20
Category: python
Slug: python3_lib_os.path

目的:文件名和路径解析,组合,测试等。

使用os.path模块中包含的函数编写代码以便在多个平台上处理文件非常简单。即使不打算在平台之间移植的程序也应该使用os.path来进行可靠的文件名解析。

解析路径

路径解析依赖于在os中定义的几个变量:

os.sep - 路径各部分之间的分隔符(例如“/”或“\”)。

os.extsep - 文件名和文件扩展名之间的分隔符。

os.pardir - 上级目录(e.g., “..”)。

os.curdir - 当前目录 (e.g., “.”)。

split()函数将路径分成两个独立的部分,并返回包含结果的元组。元组的第二个元素是路径的最后一个元素,第一个元素是之前的所有元素。


# ospath_split.py

import os.path

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

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

执行结果


$ python3 ospath_split.py

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

basename()函数返回等价于split()第二部分的值。


import os.path

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

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

执行结果


$ python3 ospath_basename.py

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

完整路径被分解到最后一个元素,无论是指文件还是目录。如果路径在目录分隔符(os.sep)中结束,则basename是空的。

dirname() 函数返回分割路径的第一部分:


import os.path

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

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

执行结果


$ python3 ospath_dirname.py

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

将basename()的结果与dirname()结合起来得到原始路径。

splitext()工作方式类似于split(),但将扩展分隔符(而不是目录分隔符)的路径分开。


import os.path

PATHS = [
    '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)))

执行结果


$ python3 ospath_splitext.py

       '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', '.')

在查找扩展名时,只使用最后一次出现的os.extsep,所以如果文件名有多个扩展名,则将其分割结果保留在前缀的一部分。

commonprefix() 将路径列表作为参数,并返回表示所有路径中存在的公共前缀的单个字符串。该值可能表示实际上不存在的路径,并且路径分隔符不包含在考虑因素中,所以前缀可能不会停止在分隔符边界上。


import os.path

paths = ['/one/two/three/four',
         '/one/two/threefold',
         '/one/two/three/',
         ]
for path in paths:
    print('PATH:', path)

print()
print('PREFIX:', os.path.commonprefix(paths))

执行结果


$ python3 ospath_commonprefix.py

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

PREFIX: /one/two/three

commonpath()则会考虑os.seq。


import os.path

paths = ['/one/two/three/four',
         '/one/two/threefold',
         '/one/two/three/',
         ]
for path in paths:
    print('PATH:', path)

print()
print('PREFIX:', os.path.commonpath(paths))

执行结果


$ python3 ospath_commonpath.py

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

PREFIX: /one/two

本文代码地址

本文最新版本地址

交流QQ群:python 测试开发 144081101

构建路径

除了将现有的路径分开之外,通常还需要从其他字符串构建路径。要将多个路径组件合并请使用join():


import os.path

PATHS = [
    ('one', 'two', 'three'),
    ('/', 'one', 'two', 'three'),
    ('/one', '/two', '/three'),
]

for parts in PATHS:
    print('{} : {!r}'.format(parts, os.path.join(*parts)))

执行结果


$ python3 ospath_join.py

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

如果任何连接的参数以os.sep开始,则所有先前的参数都将被丢弃,新的参数将成为返回值的开始。

也可以使用包含可自动扩展的“可变”组件的路径。例如,expanduser() 将 ~转换为用户主目录的名称。


# ospath_expanduser.py

import os.path

for user in ['', 'andrew', 'nosuchuser']:
    lookup = '~' + user
    print('{!r:>15} : {!r}'.format(
        lookup, os.path.expanduser(lookup)))

执行结果


            '~' : '/home/andrew'
      '~andrew' : '/home/andrew'
  '~nosuchuser' : '~nosuchuser'

如果找不到用户的主目录,则字符串将以不变的形式返回,如本示例中的〜nosuchuser一样。

expandvars()更通用,它展开路径中的所有shell环境变量。


# ospath_expandvars.py

import os.path
import os

os.environ['MYVAR'] = 'VALUE'

print(os.path.expandvars('/path/to/$MYVAR'))

执行结果


$ python3 ospath_expandvars.py

/path/to/VALUE

规范化路径

使用join()或嵌入变量从不同的字符串汇编的路径可能会以额外的分隔符或相对路径组件结束。使用normpath()来清理它们:


# ospath_normpath.py

import os.path

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

for path in PATHS:
    print('{!r:>22} : {!r}'.format(path, os.path.normpath(path)))

执行结果


$ python3 ospath_normpath.py

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

要将相对路径转换为绝对文件名,请使用abspath()。


# ospath_abspath.py

import os
import os.path

os.chdir('/usr')

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

for path in PATHS:
    print('{!r:>21} : {!r}'.format(path, os.path.abspath(path)))

执行结果


$ python3 ospath_normpath.py

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

文件时间

除了使用路径之外,os.path还包括用于检索文件属性的函数,类似于os.stat()返回的函数:


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__))

os.path.getatime()返回访问时间,os.path.getmtime()返回修改时间,os.path.getctime()返回创建时间。 os.path.getsize()返回文件中的数据量,用字节表示。

执行结果


$ python3 ospath_properties.py

File         : ospath_properties.py
Access time  : Fri Aug 26 16:38:05 2016
Modified time: Fri Aug 26 15:50:48 2016
Change time  : Fri Aug 26 15:50:49 2016
Size         : 481

测试文件

当程序遇到路径名时,通常需要知道路径是指文件,目录还是符号链接,以及是否存在。 os.path包含测试所有这些条件的函数。


import os.path

FILENAMES = [
    __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))
    print('Link Exists?:', os.path.lexists(file))
    print()

执行结果


$ ln -s /does/not/exist broken_link
$ python3 ospath_tests.py

File        : 'ospath_tests.py'
Absolute    : False
Is File?    : True
Is Dir?     : False
Is Link?    : False
Mountpoint? : False
Exists?     : True
Link Exists?: True

File        : ''
Absolute    : False
Is File?    : False
Is Dir?     : False
Is Link?    : False
Mountpoint? : False
Exists?     : False
Link Exists?: False

File        : '/'
Absolute    : True
Is File?    : False
Is Dir?     : True
Is Link?    : False
Mountpoint? : True
Exists?     : True
Link Exists?: True

File        : './broken_link'
Absolute    : False
Is File?    : False
Is Dir?     : False
Is Link?    : True
Mountpoint? : False
Exists?     : False
Link Exists?: True

参考资料