os.path 提供了一些处理文件路径的函数.

os.path.abspath(path)

返回绝对路径, 在大多数平台上, os.path.abspath(path) == os.path.normpath(os.path.join(os.getcwd(), path))

os.path.basename(path)

返回路径 basename, 是 os.path.split(path) 返回元组的第二项

>>> os.path.basename("/foo/bar/")

""

>>> os.path.basename("/foo/bar")

"bar"

os.path.commonprefix(list)

返回列表中最长的路径前缀, 如果列表为空则返回空字符串, 由于是 character-by-character, 可能会返回非法前缀

>>> os.path.commonprefix([])

""

>>> os.path.commonprefix(["/usr/local/etc/redis.conf", "/usr/local/etc/rc.d/init.d/", "/usr/local/except/"])

"/usr/local/e"

os.path.dirname(path)

返回目录, 是 os.path.split(path) 返回元组的第一项

os.path.exists(path)

路径是否存在, broken symbolic links 返回 False, 权限不够也会返回 False

>>> os.path.exists("/usr/local/etc/redis.conf")

True

>>> os.path.exists("/root/install.log")    # /root/install.log really exists

False

os.path.lexists(path)

路径是否存在, broken symbolic links 返回 True, 权限不够返回 False

os.path.expanduser(path)

替换 path 中的 ~ 为 $HOME 或 `pwd`

os.path.getatime(path)

返回路径上一次访问时间

>>> os.path.getatime("auth.py")
1472808267.0

os.path.getmtime(path)

返回路径上一次修改时间

>>> os.path.getmtime("auth.py")
1471408453.0

os.path.getctime(path)

在类 Unix 系统中返回路径上一次 metadata 变更时间, os.path.getctime(path) == os.path.getmtime(path)

os.path.getsize(path)

Return the size, in bytes, of path. 如果 path 是目录, 返回值并不定于目录中文件的大小

os.path.isabs(path)

判断是否为绝对地址, 在类 Unix 系统中意味着路径以 / 开始

>>> os.path.isabs("/watchdog")
True
>>> os.path.isabs("watchdog")
False

os.path.isfile(path)

判断 path 是否是文件, 如果 path 是链接, 会追踪至文件, 如果文件存在则 True, 若不存在则 False, 对于一个是链接的 path, 会存在 os.path.isfile(path) 和 os.path.islink(path) 同时为 True 的情况

os.path.isdir(path)

判断 path 是否是目录, os.path.isdir(path) 和 os.path.islink(path) 可同时为 True

os.path.islink(path)

判断 path 是否是链接

os.path.ismount(path)

判断 path 是否是挂载点

os.path.join(path, *paths)

连接路径, 并没有觉得有多聪明

>>> os.path.join("home/", "home/work/", "watchdog")
'home/home/work/watchdog'
>>> os.path.join("home/", "/work/", "watchdog")
'/work/watchdog'
>>> os.path.join("home/", "work/", "watchdog")
'home/work/watchdog'
>>> os.path.join("/home/", "work/", "watchdog")
'/home/work/watchdog'
>>> os.path.join("/home/", "/work/", "watchdog")
'/work/watchdog'
>>> os.path.join("/home/", "/home/work/", "watchdog")
'/home/work/watchdog'

os.path.realpath(path)

返回真实路径, 链接会追踪至文件

os.path.relpath(path[, start])

返回当前目录或 start 开始的相对路径, 路径有效性不做判断

os.path.samefile(path1, path2)

如果 path1 和 path2 指向相同的目录或文件, 返回 True

os.path.sameopenfile(fp1, fp2)

如果 fp1 和 fp2 指向相同的文件, 返回 True

os.path.split(path)

将 path 拆分为 head 和 tail, tail 是最后一个 / 之后的内容, head 是最后一个 / 之前的内容

os.path.walk(path, visit, arg)

遍历path, Python 3 中被 os.walk 取代

os.walk(toptopdown=Trueonerror=Nonefollowlinks=False)

Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.'and '..'). filenames is a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath,name).

python os.path的更多相关文章

  1. Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数

    Python  os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...

  2. python os.path 的使用

    import os #该文件所在位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py path1 = os.path.dirname(__file__) print(path1)#获 ...

  3. Python os.path.dirname(__file__) os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  4. 【308】Python os.path 模块常用方法

    参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01   abspath 返回一个目录的绝对路径. 02   basename 返回一个目录的基名 ...

  5. Python——os.path.dirname(__file__) 与 os.path.join(str,str)

    Python os.path.dirname(__file__) Python os.path.join(str,str)   (1).当"print os.path.dirname(__f ...

  6. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

  7. [python] os.path说明

    os.path - Common pathname manipulations操作 This module implements some useful functions on pathnames. ...

  8. python - os.path,路径相关操作

    python处理系统路径的相关操作: # -*- coding: utf-8 -*- import os # 属性 print '__file__: %s' % __file__ # 绝对路径(包含文 ...

  9. python os.path模块--转载

    os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...

随机推荐

  1. WPF实现物理效果 拉一个小球

    一直以来都对物理效果有神秘感,完全不知道怎么实现的.直到看到了周银辉在老早前写的一篇博客:http://www.cnblogs.com/zhouyinhui/archive/2007/06/23/79 ...

  2. 使用s3cmd操作ceph rgw

    安装1.sudo apt-get install -y python-pip sudo pip install s3cmd 2. sudo apt-get install s3cmd   配置 s3c ...

  3. JavaScript零基础学习系列二

    条件控制 if(条件){//语句块}如果条件(小括号里面的)满足true.那么才会执行大括号里面的代码,如果条件不满足(false),那么不执行,注意:有可能代码不会执行. 例如: if(3>1 ...

  4. SVM(支持向量机)的一点理解

    最近有被问到SVM的问题,不懂装懂,羞愧不已.百度有很多深入浅出介绍SVM的文章,我就不赘述了,这里写一点自己肤浅的理解. SVM的核心思想是把求解低维空间上的高维分类器转化为求解高维函数空间上的线性 ...

  5. [转]win 10 开始菜单(Win 7风格)增强工具 StartIsBack++ v1.3.4 简体中文特别版

    Windows10开始菜单增强工具StartIsBack++现已更新至v1.3.4,最近主要修复在Win10周年更新版上恢复睡眠后任务栏通知中心按钮消失的问题.升级版对StartIsBack+全新构建 ...

  6. VS2010出现FileTracker : error FTK1011编译错误的解决办法

    VS2010出现FileTracker : error FTK1011不知道是不是vs2010的一个bug,反正有人提交了. FileTracker : error FTK1011编译错误的解决办法有 ...

  7. java之线程

    java之线程 一:线程: 线程是什么呢?线程,有时被称为轻量级进程是程序执行流的最小单元.一个标准的线程由线程ID,当前指令指针(PC),寄存器集合和堆栈组成.另外,线程是进程中的一个实体,是被系统 ...

  8. SpringBoot源码解析:AOP思想以及相应的应用

    spring中拦截器和过滤器都是基于AOP思想实现的,过滤器只作用于servlet,表现在请求的前后过程中:拦截器属于spring的一个组件,由spring管理, 可以作用于spring任何资源,对象 ...

  9. Samba服务配置简明笔记

    内部服务器之间拷贝数据,用root账号访问,没有做更复杂的设置. 1.用YUM安装samba服务器端及客户端: [root@tenjs05 init.d]# yum install samba sam ...

  10. <<< Tomcat运行报错IOException while loading persisted sessions: java.io.EOFException

    解决方法:将work下面的文件清空,主要是*.ser文件,或者只是删除掉session.ser即可以解决.