os.path模块主要用于文件的属性获取,在编程中经常用到,以下是该模块的几种常用方法。
更多的方法可以去查看官方文档:http://docs.python.org/library/os.path.html
# -*- coding:utf-8 -*-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者:cacho_37967865
#博客:https://blog.csdn.net/sinat_37967865
#文件:os_path_model.py
#日期:2020-04-11
#备注:os.path 模块主要用于获取文件的属性。
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import osdef deal_path():file = 'E:\zenglingwei\\test\pic\\test.jpg'print('返回绝对路径-->',os.path.abspath(file)) # E:\zenglingwei\test\pic\1.jpgprint('返回path的真实路径-->', os.path.realpath(file)) # E:\zenglingwei\test\pic\1.jpgprint('返回元组-->',os.path.split(file)) # ('E:\\zenglingwei\\test\\pic', '1.jpg')print('返回文件路径-->',os.path.dirname(file)) # E:\zenglingwei\test\picprint('返回文件名-->', os.path.basename(file)) # 1.jpgprint('返回正确、错误-->', os.path.exists(file)) # Trueprint('返回文件大小-->', os.path.getsize(file)) # 653803b 如果文件不存在就返回错误print('返回最近访问时间-->',os.path.getatime(file)) # 1586593521.9310188print('返回文件创建时间-->',os.path.getctime(file)) # 1586593521.9310188print('返回最近修改时间-->',os.path.getmtime(file)) # 1586593521.9310188print('合并绝对路径-->',os.path.join(os.path.dirname(file),os.path.basename(file))) # E:\zenglingwei\test\pic\1.jpgprint('合并绝对路径-->', os.path.join('data','logs','test')) # data\logs\testif __name__ == '__main__':deal_path()