os.path.join(path, *paths)
将多个路径结合,建立一个新路径。
>>> os.path.join('c:\\phpStudy','x.html')
'c:\\phpStudy\\x.html'
>>> os.path.join('c:\\phpStudy','www','index.php')
'c:\\phpStudy\\www\\index.php'
os.path.abspath(path)
绝对路径。
>>> import os
>>> os.chdir('C:\LPM')#LearnPythonModule
>>> os.path.abspath('.')
'C:\\LPM'
>>> os.path.abspath('./test.py')
'C:\\LPM\\test.py'
os.path.split(path)
切分路径的目录、文件名。
>>> import os.path
>>> os.path.split('C:/phpStudy/x.html')
('C:/phpStudy', 'x.html')
>>> os.path.split('C:/phpStudy/')
('C:/phpStudy', '')
os.path.splitext(path)
类似os.path.split(),不过splitext是根据扩展名分割路径。
>>> os.path.splitext('C:/phpStudy/x.html')
('C:/phpStudy/x', '.html')
os.path.splitdrive(path)
将路径名路径拆分为一对(drive,tail),其中drive是装入点或空字符串。在不使用驱动器规格的系统上,驱动器始终是空字符串。在所有情况下,驱动+尾部将与路径相同。
在Windows上,将路径名拆分为驱动器/UNC SharePoint和相对路径。
如果路径包含驱动器号,则驱动器将包含冒号之前(包括冒号)的所有内容。例如splitdrive(“c:/dir”)返回(“c:”,/dir”)。
>>> os.path.splitdrive(r"C:\Users\Administrator\codecs.cd")
('C:', '\\Users\\Administrator\\codecs.cd')
os.path.dirname(path) & os.path.basename(path)
dirname得到路径目录,basename得到文件名
>>> os.path.dirname('C:/phpStudy/x.html')
'C:/phpStudy'
>>> os.path.basename('C:/phpStudy/x.html')
'x.html'
os.path.exists(path)
判断路径是否存在。
os.path.commonpath(paths)
接受一个路径列表参数,返回共同的路径
>>> os.path.commonpath(['c:/phpstudy/x.html','c:/phpstudy/x.html'])
'c:\\phpstudy\\x.html'
>>> os.path.commonpath(['c:/phpstudy/x.html','c:/phpstudy/y.html'])
'c:\\phpstudy'
os.path.commonprefix(list)
返回路径共同前缀。
>>> os.path.commonpath(['C:/phpStudy/www/index.php','C:/phpStudy/www/index_ex.php'])
'C:\\phpStudy\\www'
>>> os.path.commonprefix(['C:/phpStudy/www/index.php','C:/phpStudy/www/index_ex.php'])
'C:/phpStudy/www/index'
os.path.normpath(path)
规范化路径:清除多余的分隔符或相对路径部分。
>>> os.path.normpath('c:/phpStudy/www/../x.html')
'c:\\phpStudy\\x.html'
文件时间
os.path.getatime(path)------返回访问时间
os.path.getmtime(path)------返回修改时间
os.path.getctime(path)------返回创建时间
os.path.getsize(path)------返回文件中的数据量,以字节为单位
#ospath_properties.py
import os.path
import time
print('File :',__file__)
print('Access time :',time.ctime(os.path.getatime(__file__)))
print('Modified time:',time.ctime(os.path.getmtime(__file__)))
print('Change time :',time.ctime(os.path.getctime(__file__)))
print('Size :',os.path.getsize(__file__))
"""运行结果
File : ospath_properties.py
Access time : Tue Jan 15 14:05:58 2019
Modified time: Tue Jan 15 14:10:12 2019
Change time : Tue Jan 15 14:05:58 2019
Size : 306
"""
测试文件
os.path.isabs(path):path是否为绝对路径。
os.path.isfile(path):path是否是一个存在的文件。
os.path.isdir(path):path是否为一个存在的目录。
os.path.islink(path):是否是存在的链接目录文件
os.path.lexists(path):与os.exists()类似,如果path指向现有路径,则返回True。
#ospath_tests.py
import os.path
print('File :',__file__)
print('Absolute :',os.path.isabs(__file__))
print('Is File? :',os.path.isfile(__file__))
print('Is Dir? :',os.path.isdir(__file__))
print('Is Link? :',os.path.islink(__file__))
print('MountPoint :',os.path.ismount(__file__))
print('Exists? :',os.path.exists(__file__))
print('Link Exists?:',os.path.lexists(__file__))
"""运行结果
File : ospath_tests.py
Absolute : False
Is File? : True
Is Dir? : False
Is Link? : False
MountPoint : False
Exists? : True
Link Exists?: True
"""