python os.path模块用法详解
abspath
返回一个目录的绝对路径
Return an absolute path.
>>> os.path.abspath("/etc/sysconfig/selinux")
'/etc/sysconfig/selinux'
>>> os.getcwd()
'/root'
>>> os.path.abspath("python_modu")
'/root/python_modu'
basename
返回一个目录的基名
Returns the final component of a pathname
>>> os.path.basename("/etc/sysconfig/selinux")
'selinux'
>>> os.path.basename("/usr/local/python3/bin/python3")
'python3'
dirname
返回一个目录的目录名
Returns the directory component of a pathname
>>> os.path.dirname("/etc/sysconfig/selinux")
'/etc/sysconfig'
>>> os.path.dirname("/usr/local/python3/bin/python3")
'/usr/local/python3/bin'
exists
测试指定文件是否存在
Test whether a path exists. Returns False for broken symbolic links
>>> os.path.exists("/home/egon")
False
>>> os.path.exists("/root")
True
>>> os.path.exists("/usr/bin/python")
True
getatime
得到指定文件最后一次的访问时间
Return the last access time of a file, reported by os.stat().
>>> os.stat("/root/test.sh")
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)
>>> os.path.getatime("/root/test.sh")
1498117664.2808378
getctime
得到指定文件最后一次的改变时间
Return the metadata change time of a file, reported by os.stat().
>>> os.stat("/root/test.sh")
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)
>>> os.path.getctime("/root/test.sh")
1498117696.039542
getmtime
得到指定文件最后一次的修改时间
Return the last modification time of a file, reported by os.stat().
>>> os.stat("/root/test.sh")
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)
>>> os.path.getmtime("/root/test.sh")
1496629059.9313989
getsize
得到得到文件的大小
Return the size of a file, reported by os.stat().
>>> os.stat("/root/test.sh")
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)
>>> os.path.getsize("/root/test.sh")
568
isabs
测试参数是否是绝对路径
Test whether a path is absolute
>>> os.path.isabs("python_modu")
False
>>> os.path.isabs("/etc/sysconfig")
True
isdir
测试指定参数是否是目录名
Return true if the pathname refers to an existing directory.
>>> os.path.isdir("/etc/sysconfig/selinux")
False
>>> os.path.isdir("/home")
True
isfile
测试指定参数是否是一个文件
Test whether a path is a regular file
>>> os.path.isfile("/home")
False
>>> os.path.isfile("/etc/sysconfig/selinux")
True
islink
测试指定参数是否是一个软链接
Test whether a path is a symbolic link
>>> os.path.islink("/etc/sysconfig/selinux")
True
>>> os.path.islink("/etc/sysconfig/nfs")
False
ismount
测试指定参数是否是挂载点
Test whether a path is a mount point
>>> os.path.ismount("/mnt/cdrom")
False
以上是未挂载光盘,现在把光盘挂载到/mnt/cdrom下,再进行测试
>>> os.path.ismount("/mnt/cdrom")
True
join
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.
>>> for filename in os.listdir("/home"):
... print(os.path.join("/tmp",filename))
...
/tmp/a
/tmp/f1.txt
realpath
返回指定文件的标准路径,而非软链接所在的路径
Return the canonical path of the specified filename, eliminating any
symbolic links encountered in the path.
>>> os.path.realpath("/etc/sysconfig/selinux")
'/etc/selinux/config'
>>> os.path.realpath("/usr/bin/python")
'/usr/bin/python2.7'
samefile
测试两个路径是否指向同一个文件
Test whether two pathnames reference the same actual file
sameopenfile
测试两个打开的文件是否指向同一个文件
Test whether two open file objects reference the same file
split
分割目录名,返回由其目录名和基名给成的元组
Split a pathname. Returns tuple "(head, tail)" where "tail" is
everything after the final slash. Either part may be empty.
>>> os.path.split("/tmp/f1.txt")
('/tmp', 'f1.txt')
>>> os.path.split("/home/test.sh")
('/home', 'test.sh')
splitext
分割文件名,返回由文件名和扩展名组成的元组
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.
>>> os.path.splitext("/home/test.sh")
('/home/test', '.sh')
>>> os.path.splitext("/tmp/f1.txt")
('/tmp/f1', '.txt')
python os.path模块用法详解的更多相关文章
- python os.path模块常用方法详解
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...
- python os.path模块常用方法详解(转)
转自:https://www.cnblogs.com/wuxie1989/p/5623435.html os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方 ...
- python os.path模块常用方法详解 ZZ
os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法.更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.ht ...
- python os.path模块常用方法详解:转:http://wangwei007.blog.51cto.com/68019/1104940
1.os.path.abspath(path) 返回path规范化的绝对路径. >>> os.path.abspath('test.csv') 'C:\\Python25\\test ...
- 【python基础】os.path模块常用方法详解
os.path模块 主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法. 更多的方法可以去查看官方文档:http://docs.python.org/library/os.path. ...
- 24.python中xlwt模块用法详解
1.创建并保存一个excel 创建一个工作簿,设置编码格式为“utf-8”,默认格式是ASCII,为了方便写入中文,一般都要设置成UTF-8 import xlwt wb = xlwt.Workboo ...
- Python Deque 模块使用详解,python中yield的用法详解
Deque模块是Python标准库collections中的一项. 它提供了两端都可以操作的序列, 这意味着, 你可以在序列前后都执行添加或删除. https://blog.csdn.net/qq_3 ...
- 【308】Python os.path 模块常用方法
参考:Python os.path 模块 参考:python3中,os.path模块下常用的用法总结 01 abspath 返回一个目录的绝对路径. 02 basename 返回一个目录的基名 ...
- python os.path 模块
os.path模块用法: 1, os.path.basename() >>> os.path.basename('/share/Public/cmiao')'cmiao' basen ...
随机推荐
- 怎么让BarTender对象等间距分布
在BarTender 2016设计条码标签时,我们需要让对象分布尽可能整齐美观,例如实现对象的对齐,对象等间距分布等.这些在作为世界上最好且最受信任的条码打印软件BarTender中,都是可以很轻松的 ...
- VC++关于UNICODE版本的开发
关于UNICODE版本的开发 代码转换方案 概述 在VC6.0中,相应的有一些宏来代替ANSI的函数.宏或数据类型,这些宏在ANSI编译条件中处理字符串为单字节,而在UNICODE中处理字符串为双字节 ...
- javascript生成m位随机数
根据时间生成m位随机数,最大13位随机数,并且不能保证首位不为0 function ran(m) { m = m > 13 ? 13 : m; var num = new Date().getT ...
- bash脚本 while语法
基本语法(比较常见的两种形式): 只要特定条件为真,”while” 语句就会执行 while [ condition ] do command1 command2 command3 done 或者 w ...
- 8 -- 深入使用Spring -- 3...1 Resource实现类InputStreamResource、ByteArrayResource
8.3.1 Resource实现类------InputStreamResource:访问输入流资源的实现类.ByteArrayResource:访问字节数组资源的实现类. 5. 访问字节数组资源 ⊙ ...
- javascript的replace方法的高级应用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- gem install cocoapods ERROR: While executing gem ... (Gem::FilePermissionError)
在cocoapods 执行 sudo gem install cocoapods 的时候出现 While executing gem ... (Gem::FilePermissionError) ...
- Excel导出到浏览器(个人备份)
/// <summary> /// Excel导出 /// </summary> /// <param name="dt"></pa ...
- Python标准异常和异常处理详解
python提供了两个非常重要的功能来处理python程序在运行中出现的异常和错误.你可以使用该功能来调试python程序. 1.异常处理: 本站Python教程会具体介绍. 2.断言(Asserti ...
- 【技术分享会】 @第二期 微信开放API简述-0212
什么是微信开放平台? 微信开放平台作为第三方移动程序提供接口,使用户可将第三方程序的内容发布给好友或分享至朋友圈,第三方内容借助微信平台获得更广泛的传播.从而形成了一种主流的线上线下微信互动营销方式. ...