python 中 os.path模块

发布时间 2023-07-07 16:54:14作者: 小鲨鱼2018

 

001、basename:去掉路径名,单独获取文件名

>>> import os.path
>>> os.getcwd()
'/home/test02'
>>> os.listdir()
['a.txt', 'test_dir']
>>> os.path.basename("/home/test02/a.txt")       ## 去掉路径名,单独获取文件名
'a.txt'

 

002、dirname:去掉文件名,单独获取路径名

>>> os.getcwd()
'/home/test02'
>>> os.listdir()
['a.txt', 'test_dir']
>>> os.path.dirname("/home/test02/a.txt")     ## 去掉文件名,单独获取路径
'/home/test02'

 

003、