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. ReactNative官方中文文档0.21

    整理了一份ReactNative0.21中文文档,提供给需要的reactnative爱好者.ReactNative0.21中文文档.chm  百度盘下载:ReactNative0.21中文文档 来源: ...

  2. Caliburn.Micro学习笔记(四)----IHandle<T>实现多语言功能

    Caliburn.Micro学习笔记目录 说一下IHandle<T>实现多语言功能 因为Caliburn.Micro是基于MvvM的UI与codebehind分离, binding可以是双 ...

  3. 基于C/S架构的3D对战网络游戏C++框架 _02系统设计(总体设计、概要设计)

    本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...

  4. 建模算法(七)——排队论模型

    (一)基本概念 一.排队过程的一般表示 凡是要求服务的对象称为顾客,凡是为顾客服务的称为服务员 二.排队系统的组成和特征 主要由输入过程.排队规则.服务过程三部分组成 三.排队模型的符号表示 1.X: ...

  5. java中使用MD5加密技术

    在项目中经常会对一些信息进行加密,现在常用的信息加密技术有:MD5.RSA.DES等,今天主要说一下,md5加密,以及如何在java代码根据自己的业务需求使用md5. MD5简介: MD5即Messa ...

  6. smarty下如何将一个数保存为两位小数

    smarty模板是一种缓存技术,下面介绍一下smarty string_format用法 取小数点后2位: 用法如下: //index.php$smarty = new Smarty; $smarty ...

  7. 黄聪:phpexcel中文教程-设置表格字体颜色背景样式、数据格式、对齐方式、添加图片、批注、文字块、合并拆分单元格、单元格密码保护

    首先到phpexcel官网上下载最新的phpexcel类,下周解压缩一个classes文件夹,里面包含了PHPExcel.php和PHPExcel的文件夹,这个类文件和文件夹是我们需要的,把class ...

  8. 在php中验证复选框

    PHP接收多个同名复选框信息不像ASP那样自动转换成为数组,这给使用带来了一定不便.但是还是有解决办法的,就是利用javascript做一下预处理.多个同名复选框在javascript中还是以数组的形 ...

  9. hdu5000 背包dp

    题意可抽象为:N个包中每个包容量是T[i],每个包都拿一些,设拿出的总数为sum时的方案数为q,求max(q) 设dp[i][j]为拿了前i个包,共拿出了j物品时的方案数.那么 for i=1 to ...

  10. AI贪吃蛇(二)

    前言 之前写过一篇关于贪吃蛇AI的博客,当时虽然取得了一些成果,但是也存在许多问题,所以最近又花了三天时间重新思考了一下.以下是之前博客存在的一些问题: 策略不对,只要存在找不到尾巴的情况就可能失败, ...