Python os.path

来源:互联网 发布:evernote mac 编辑:程序博客网 时间:2024/06/02 01:58
一 分隔:
1 basename()  #去掉目录路径,返回文件名
#用法如:
 >>>os.path.basename("c:\temp\test.txt")
'test.txt'

2 dirname()  #去掉文件名,返回目录路径
#用法如:
 >>>os.path.dirname("c:\temp\test.txt")
'c:\\temp'

3 join() #将分离的各部分组合成一个路径名
#用法如:
 >>>os.path.join("c:\temp\","test.txt")
'c:\\temp\\test.txt'

4 split() #返回 目录路径和文件名的元组
>>>os.path.split("c:\temp\","test.txt")
('c:\\temp','test.txt')

5 splitdrive() #返回 驱动符号和路径字符元组
>>>os.path.splitdrive("c:\temp\","test.txt")
('c:','\\temp\\test.txt')

6 splitext() #返回文件名和扩展名元组
>>>os.path.splitext("test.txt")
('test','txt')

二 信息:
1 getatime() #返回文件最近的访问时间
>>>os.path.getatime("c:\temp\test.txt")
1281154109.6167181
#这里的时间以秒为单位,并且从1970年1月1日开始算起。为了获取以天为单位的最后访问日期,可以使用下列代码: 
import time # time.time()返回当前时间 
age_in_days = (time.time()-time_of_last_access)/(60*60*24) 

2 getctime() #返回文件的创建时间

3 getmtime() #返回文件的创建时间

4 getsize() #返回文件的大小 单位为字节
>>>os.path.getsize("c:\temp\test.txt")
1281L

三 查询:
1 exists() #指定路径(文件或目录)是否存在
>>>os.path.exists("c:\temp\test.txt")
True

2 isabs() #指定路径是否为绝对路径

3 isdir() #指定路径是否存在且为一个目录

4 isfile() #指定的路径是否为一个文件

5 samefile() #两个路径名是否指向同一个文件
0 0