Python os.path模块

一.

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() #两个路径名是否指向同一个文件
原文地址:https://www.cnblogs.com/jp927/p/4536400.html