python  os.path路径的聚合-分割-遍历方法

版权声明:本文为博主 一銤阳光 学习整理的文章,如需转载,请注明出处、附上CSDN博文链接。 https://blog.csdn.net/CSDNhuaong/article/details/84890075

写在前面

在python代码中,处理文件经常涉及到路径的操作,os.path提供了路径操作的多种方法。
通过 pydoc os.path 可以查看到

Help on module posixpath in os:

NAME
   posixpath - Common operations on Posix pathnames.

FILE   
/home/hualong/ssd/software/anaconda2/lib/python2.7/posixpath.py

MODULE DOCS
   https://docs.python.org/library/posixpath

DESCRIPTION
   Instead of importing this module directly, import os and refer to
   this module as os.path.  The "os.path" name is an alias for this
   module on Posix systems; on other systems (e.g. Mac, Windows),
   os.path provides the same operations in a manner specific to that
   platform, and is an alias to another module (e.g. macpath, ntpath).
   
   Some of this can actually be useful on non-Posix systems too, e.g.
   for manipulation of the pathname component of URLs.

FUNCTIONS
   abspath(path)  //绝对路径
       Return an absolute path.
   
   basename(p)  //返回路径中最后一个部分,通常也就是文件名了
       Returns the final component of a pathname
   
   commonprefix(m)
       Given a list of pathnames, returns the longest common leading component
   
   dirname(p)
       Returns the directory component of a pathname
   
   exists(path)
       Test whether a path exists.  Returns False for broken symbolic links
   
   expanduser(path)
       Expand ~ and ~user constructions.  If user or $HOME is unknown,
       do nothing.
   
   expandvars(path)
       Expand shell variables of form $var and ${var}.  Unknown variables
       are left unchanged.
   
   getatime(filename)
       Return the last access time of a file, reported by os.stat().
   
   getctime(filename)
       Return the metadata change time of a file, reported by os.stat().
   
   getmtime(filename)
       Return the last modification time of a file, reported by os.stat().
   
   getsize(filename)
       Return the size of a file, reported by os.stat().
   
   isabs(s)
       Test whether a path is absolute
   
   isdir(s)
   Return true if the pathname refers to an existing directory.
   
   isfile(path)
       Test whether a path is a regular file
   
   islink(path)
       Test whether a path is a symbolic link
   
   ismount(path)
       Test whether a path is a mount point
   
   join(a, *p) //路径拼接
       Join two or more pathname components, inserting '/' as needed.
       If any component is an absolute path, all previous path components
       will be discarded.  An empty last part will result in a path that
       ends with a separator.
   
   lexists(path)
       Test whether a path exists.  Returns True for broken symbolic links
   
   normcase(s)
       Normalize case of pathname.  Has no effect under Posix
   
   normpath(path)
       Normalize path, eliminating double slashes, etc.
   
   realpath(filename)
       Return the canonical path of the specified filename, eliminating any
       symbolic links encountered in the path.
   
   relpath(path, start='.')
       Return a relative version of a path
   
   samefile(f1, f2)
       Test whether two pathnames reference the same actual file
   
   sameopenfile(fp1, fp2)
       Test whether two open file objects reference the same file
   
   samestat(s1, s2)
       Test whether two stat buffers reference the same file
       
split(p) //路径分割
       Split a pathname.  Returns tuple "(head, tail)" where "tail" is
       everything after the final slash.  Either part may be empty.
   
   splitdrive(p)
       Split a pathname into drive and path. On Posix, drive is always
       empty.
   
   splitext(p)
       Split the extension from a pathname.
       
       Extension is everything from the last dot to the end, ignoring
       leading dots.  Returns "(root, ext)"; ext may be empty.
   
   walk(top, func, arg)  //路径的遍历
       Directory tree walk with callback function.
       
       For each directory in the directory tree rooted at top (including top
       itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
       dirname is the name of the directory, and fnames a list of the names of
       the files and subdirectories in dirname (excluding '.' and '..').  func
       may modify the fnames list in-place (e.g. via del or slice assignment),
       and walk will only recurse into the subdirectories whose names remain in
       fnames; this can be used to implement a filter, or to impose a specific
       order of visiting.  No semantics are defined for, or required of, arg,
       beyond that arg is always passed to func.  It can be used, e.g., to pass
       a filename pattern, or a mutable object designed to accumulate
       statistics.  Passing None for arg is common.

DATA
   __all__ = ['normcase', 'isabs', 'join', 'splitdrive', 'split', 'splite...
   altsep = None
   curdir = '.'
   defpath = ':/bin:/usr/bin'
   devnull = '/dev/null'
   extsep = '.'
   pardir = '..'
   pathsep = ':'
   sep = '/'
   supports_unicode_filenames = False

(END)



os.path常用方法的使用说明

  • 路径聚合
  • 路径拆分

# -*- coding:utf-8 -*-
import os

## 路径聚合
os.path.join(prefix, file)
os.path.join(dir_path, os.path.join('rgb_images', label)) 
filename=os.path.join('/home/ubuntu/code',  'split_func')
## 稍微注意 字符串前面是否加 '/'

##路径切分  根据需求有 splitext 和 split
#os.path.splitext()将文件名和扩展名分开
#os.path.split()返回文件的路径和文件名

fname,fename=os.path.splitext('/home/ubuntu/python_coding/split_func/split_function.py')
print 'fname is:',fname  ##路径和文件名
print 'fename is:',fename   ##只剩下扩展名
#输出为:
# fname is:/home/ubuntu/python_coding/split_func/split_function
#fename is:.py

#os.path.split()返回文件的路径和文件名
dirname,filename=os.path.split('/home/ubuntu/python_coding/split_func/split_function.py')
print dirname ##前端路径
print filename  ##文件名和扩展名
#输出为:
# /home/ubuntu/python_coding/split_func
#split_function.py

## 如何还不能满足拆分需求,可以使用 字符串切分
#split()函数
#string.split(str="", num=string.count(str))[n]
#str - - 分隔符,默认为所有的空字符,包括空格、换行(\n)、制表符(\t)等。
#num - - 分割次数。
#[n] - - 选取的第n个分片
string = "hello.world.python"
print string.split('.')#输出为:['hello', 'world', 'python']
print(string.split('.',1))#输出为:['hello', 'world.python']
print(string.split('.',1)[0])#输出为:hello
print(string.split('.',1)[1])#输出为:world.python


参考文档

mark_leiliu-CSDN https://blog.csdn.net/T1243_3/article/details/80170006

猜你喜欢

转载自blog.csdn.net/CSDNhuaong/article/details/84890075