abspath

  1. 返回一个目录的绝对路径
  2. Return an absolute path.
  1. >>> os.path.abspath("/etc/sysconfig/selinux")
  2. '/etc/sysconfig/selinux'
  3. >>> os.getcwd()
  4. '/root'
  5. >>> os.path.abspath("python_modu")
  6. '/root/python_modu'

basename

  1. 返回一个目录的基名
  2. Returns the final component of a pathname
  1. >>> os.path.basename("/etc/sysconfig/selinux")
  2. 'selinux'
  3. >>> os.path.basename("/usr/local/python3/bin/python3")
  4. 'python3'

dirname

  1. 返回一个目录的目录名
  2. Returns the directory component of a pathname
  1. >>> os.path.dirname("/etc/sysconfig/selinux")
  2. '/etc/sysconfig'
  3. >>> os.path.dirname("/usr/local/python3/bin/python3")
  4. '/usr/local/python3/bin'

exists

  1. 测试指定文件是否存在
  2. Test whether a path exists. Returns False for broken symbolic links
  1. >>> os.path.exists("/home/egon")
  2. False
  3. >>> os.path.exists("/root")
  4. True
  5. >>> os.path.exists("/usr/bin/python")
  6. True

getatime

  1. 得到指定文件最后一次的访问时间
  2. Return the last access time of a file, reported by os.stat().
  1. >>> os.stat("/root/test.sh")
  2. os.stat_result(st_mode=33261, st_ino=100684935, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=568, st_atime=1498117664, st_mtime=1496629059, st_ctime=1498117696)
  3. >>> os.path.getatime("/root/test.sh")
  4. 1498117664.2808378

getctime

  1. 得到指定文件最后一次的改变时间
  2. Return the metadata change time of a file, reported by os.stat().
  1. >>> os.stat("/root/test.sh")
  2. os.stat_result(st_mode=33261, st_ino=100684935, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=568, st_atime=1498117664, st_mtime=1496629059, st_ctime=1498117696)
  3. >>> os.path.getctime("/root/test.sh")
  4. 1498117696.039542

getmtime

  1. 得到指定文件最后一次的修改时间
  2. Return the last modification time of a file, reported by os.stat().
  1. >>> os.stat("/root/test.sh")
  2. os.stat_result(st_mode=33261, st_ino=100684935, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=568, st_atime=1498117664, st_mtime=1496629059, st_ctime=1498117696)
  3. >>> os.path.getmtime("/root/test.sh")
  4. 1496629059.9313989

getsize

  1. 得到得到文件的大小
  2. Return the size of a file, reported by os.stat().
  1. >>> os.stat("/root/test.sh")
  2. os.stat_result(st_mode=33261, st_ino=100684935, st_dev=2050, st_nlink=1, st_uid=0, st_gid=0, st_size=568, st_atime=1498117664, st_mtime=1496629059, st_ctime=1498117696)
  3. >>> os.path.getsize("/root/test.sh")
  4. 568

isabs

  1. 测试参数是否是绝对路径
  2. Test whether a path is absolute
  1. >>> os.path.isabs("python_modu")
  2. False
  3. >>> os.path.isabs("/etc/sysconfig")
  4. True

isdir

  1. 测试指定参数是否是目录名
  2. Return true if the pathname refers to an existing directory.
  1. >>> os.path.isdir("/etc/sysconfig/selinux")
  2. False
  3. >>> os.path.isdir("/home")
  4. True

isfile

  1. 测试指定参数是否是一个文件
  2. Test whether a path is a regular file
  1. >>> os.path.isfile("/home")
  2. False
  3. >>> os.path.isfile("/etc/sysconfig/selinux")
  4. True

islink

  1. 测试指定参数是否是一个软链接
  2. Test whether a path is a symbolic link
  1. >>> os.path.islink("/etc/sysconfig/selinux")
  2. True
  3. >>> os.path.islink("/etc/sysconfig/nfs")
  4. False

ismount

  1. 测试指定参数是否是挂载点
  2. Test whether a path is a mount point
  1. >>> os.path.ismount("/mnt/cdrom")
  2. False
  3. 以上是未挂载光盘,现在把光盘挂载到/mnt/cdrom下,再进行测试
  4. >>> os.path.ismount("/mnt/cdrom")
  5. True

join

  1. join(a, *p)
  2. 将目录名和文件的基名拼接成一个完整的路径
  3. Join two or more pathname components, inserting '/' as needed.
  4. If any component is an absolute path, all previous path components
  5. will be discarded. An empty last part will result in a path that
  6. ends with a separator.
  1. >>> for filename in os.listdir("/home"):
  2. ... print(os.path.join("/tmp",filename))
  3. ...
  4. /tmp/a
  5. /tmp/f1.txt

realpath

  1. 返回指定文件的标准路径,而非软链接所在的路径
  2. Return the canonical path of the specified filename, eliminating any
  3. symbolic links encountered in the path.
  1. >>> os.path.realpath("/etc/sysconfig/selinux")
  2. '/etc/selinux/config'
  3. >>> os.path.realpath("/usr/bin/python")
  4. '/usr/bin/python2.7'

samefile

  1. 测试两个路径是否指向同一个文件
  2. Test whether two pathnames reference the same actual file

sameopenfile

  1. 测试两个打开的文件是否指向同一个文件
  2. Test whether two open file objects reference the same file

split

  1. 分割目录名,返回由其目录名和基名给成的元组
  2. Split a pathname. Returns tuple "(head, tail)" where "tail" is
  3. everything after the final slash. Either part may be empty.
  1. >>> os.path.split("/tmp/f1.txt")
  2. ('/tmp', 'f1.txt')
  3. >>> os.path.split("/home/test.sh")
  4. ('/home', 'test.sh')

splitext

  1. 分割文件名,返回由文件名和扩展名组成的元组
  2. Split the extension from a pathname.
  3. Extension is everything from the last dot to the end, ignoring
  4. leading dots. Returns "(root, ext)"; ext may be empty.
  1. >>> os.path.splitext("/home/test.sh")
  2. ('/home/test', '.sh')
  3. >>> os.path.splitext("/tmp/f1.txt")
  4. ('/tmp/f1', '.txt')
 
 

python os.path模块用法详解的更多相关文章

  1. python os.path模块常用方法详解

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  2. python os.path模块常用方法详解(转)

    转自:https://www.cnblogs.com/wuxie1989/p/5623435.html os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方 ...

  3. python os.path模块常用方法详解 ZZ

    os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...

  4. python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940

    1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...

  5. 【python基础】os.path模块常用方法详解

    os.path模块 主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. 更多的方法可以去查看官方文档:http://docs.python.org/library/os.path. ...

  6. 24.python中xlwt模块用法详解

    1.创建并保存一个excel 创建一个工作簿,设置编码格式为“utf-8”,默认格式是ASCII,为了方便写入中文,一般都要设置成UTF-8 import xlwt wb = xlwt.Workboo ...

  7. Python Deque 模块使用详解,python中yield的用法详解

    Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...

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

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

  9. python os.path 模块

    os.path模块用法: 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basen ...

随机推荐

  1. QT 实现QGraphicsProxyWidget对象可选择或移动(item管理实现)

    上篇博文<QT QGraphicsProxyWidget对象可选择或移动的一些tricks>介绍了实现QT QGraphicsProxyWidget对象可选择或移动的一些小的第三方技巧,但 ...

  2. redis sentinels哨兵集群环境配置

    # Redis configuration file example. # # Note that in order to read the configuration file, Redis mus ...

  3. MAC下Android的Eclipse开发环境的搭建 转自MacroCheng

    原文地址:  http://www.cnblogs.com/macro-cheng/archive/2011/09/30/android-001.html 一.Eclipse的下载 到网站:http: ...

  4. SpringBoot------注解把配置文件自动映射到属性和实体类

    1.映射到属性 package top.ytheng.demo.controller; import org.springframework.beans.factory.annotation.Valu ...

  5. 使用npm国内镜像

    嫌npm指令速度慢的童鞋可以把npm的源转换成国内的即可提高响应速度: 镜像使用方法(三种办法任意一种都能解决问题,建议使用第1或者第3种,将配置写死,下次用的时候配置还在):1.通过config命令 ...

  6. 10 -- 深入使用Spring -- 5... 实现任务的自动调度

    10.5 实现任务的自动调度 10.5.1 使用Quartz 10.5.2 在Spring中使用Quartz

  7. 8 -- 深入使用Spring -- 4...6 AOP代理:基于注解的XML配置文件的管理方式

    8.4.6 基于XML配置文件的管理方式 Spring 2.x 提供一个新的aop:命名空间来定义切面.切入点和增强处理. XML配置方式优点: ⊙ 如果应用没有使用JDK 1.5 以上版本,那么应用 ...

  8. 【Spring系列】Spring IoC

    前言 IoC其实有两种方式,一种是DI,而另一种是DL,即Dependency Lookup(依赖查找),前者是当前软件实体被动接受其依赖的其他组件被IOc容器注入,而后者是当前软件实体主动去某个服务 ...

  9. 【Ubuntu】更新系统时出现Hash校验和不符的错误(已解决)

    在使用 sudo apt-get update && sudo apt-get upgrade 命令更新系统时出现类似这样的错误信息: W: 无法下载 bzip2:/var/lib/a ...

  10. smarty直接在模板中格式化时间的方法

    smarty提供了一个获取时间戳的方法: <%$smarty.now%> 使用该方法获取到当时的时间戳之后,使用格式化修饰符data-format进行修饰: <%$smarty.no ...