Python os.path模块
来源:互联网 发布:视频语速加快软件 编辑:程序博客网 时间:2024/06/09 18:36
os.path模块
basename('文件路径') 去掉目录路径,返回fname文件名
1 import os
2 os.path.basename('/Volumes/1.mp4') #输出('1.mp4')
dirname('文件路径') 去掉文件名,返回目录路径
1 import os
2 os.path.dirname('/Volumes/1.mp4') #输出('/Volumes')
splitdrive('文件路径') 返回(drivername,fpath)元组
view sourceprint?1 import os
2 os.path.splitdrive('Volumes/1.mp4') #输出('','/Volumes/1.mp4')
os.path.split('文件路径') 分割文件名与路径;返回(fpath,fname)元组;如果完全使用目录,它也会将最后一个目录作为文件名分离,且不会判断文件或者目录是否存在
1 import os
2 os.path.split('/Volumes/1.mp4') #输出(‘/Volumes’,‘1.mp4’)
3 os.path.split('/Volumes/text') #输出(‘/Volumes’,‘text’)
os.path.splitext(“文件路径”) 分离文件名与扩展名;默认返回(fname,fextension)元组,可做分片操作
1 import os
2 fname, fextension=os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')
3 print fname,fextension #输出/Volumes/Leopard/Users/Caroline/Desktop/1 .mp4
4 os.path.splitext('/Volumes/Leopard/Users/Caroline/Desktop/1.mp4')[1:] #输出('.mp4',)
os.path.join('a','b','fname.extension')->'a/b/fname.extension' 将分离的部分组成一个路径名
1 imprort os
2 os.path.join('a','b','1.mp4') #输出#‘a/b/1.mp4’
查询:返回值True,False
exists() 指定路径(文件或者目录)是否存在
isabs() 指定路径是否为绝对路径
isdir() 指定路径是否存在且为一个目录
isfile() 指定路径是否存在且为一个文件
islink() 指定路径是否存在且为一个符号链接
ismount() 指定路径是否存在且为一个挂载点 ???
samefile() 两个路径名是否指向同一个文件
文件信息
getatime() 返回最近访问时间 (浮点型秒数)
getctime() 返回文件创建时间
getmtime() 返回最近文件修改时间
getsize() 返回文件大小 (字节为单位)
abspath() 返回绝对路径
normpath() 规范path字符串形式 ???
01 import os
02 import time
03 file='/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
04 os.path.getatime(file) #输出最近访问时间1318921018.0
05 os.path.getctime(file) #输出文件创建时间
06 os.path.getmtime(file) #输出最近修改时间
07 time.gmtime(os.path.getmtime(file)) #以struct_time形式输出最近修改时间
08 os.path.getsize(file) #输出文件大小(字节为单位)
09 os.path.abspath(file) #输出绝对路径'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
10 os.path.normpath(file) #输出'/Volumes/Leopard/Users/Caroline/Desktop/1.mp4'
python d:/pythonSrc/test/test.py
那么将输出 d:/pythonSrc/test
(2).当"print os.path.dirname(__file__)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:
python test.py
那么将输出空字符串
- Python os.path模块
- Python os.path模块
- [python] os.path模块
- python os.path模块
- Python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- python os.path模块
- Python os.path模块
- Python os.path模块
- Python os.path模块
- python os.path模块
- python os.path模块
- 数码管消影问题总结
- poj 1003
- sharepoint 2010 所有list新建按钮消失和管理员权限丢失的解决方法
- 回文日和1000以内完数
- 如何与陌生人搭讪十技巧
- Python os.path模块
- You cannot combine custom titles with other title features
- PL/SQL Developer启动时报错:“Control 'dxDockBrowserPanel' has no parent window"
- Android Tab使用总结
- python3.x中urllib的使用
- 字符转换
- sql server 分组统计数据
- Pro Oracle Database 11g RAC on Linux 读书笔记1
- 下载csv,excel文件名乱码的问题(IE和FireFox两种浏览器)