Python os.path模块

1、os.path.splitext(path)  #返回一个元祖(路经,扩展名)

例如:

>>> os.path.splitext('/home/biotrainee/first/test.txt')
('/home/biotrainee/first/test', '.txt')

而os.path.split(path) 是返回(路经,文件名.扩张名)

>>> os.path.split('/home/biotrainee/first/test.txt')
('/home/biotrainee/first', 'test.txt')

2、os.path.dirname(path) 返回文件路经的目录,也就是os.path.split(path)第一个元素,如:

>>> os.path.dirname('/home/biotrainee/first/test.txt')
'/home/biotrainee/first'

3、os.path.basename(path)返回path的最后文件名,也就是os.path.split(path)第二个元素,如:

>>> os.path.basename('/home/biotrainee/first/test.txt')
'test.txt'

4、os.path.exists(path)   如果path存在,返回True;如果path不存在,返回False。如:

>>> os.path.exists('/home/biotrainee/first/test.txt')
False
>>> os.path.exists('/home/')
True

5、os.path.isabs(path)   如果path是绝对路径,返回True。如:

>>> os.path.isabs('/home/biotrainee/first/test.txt')
True
>>> os.path.isabs('../biotrainee/first/test.txt')
False

等等等等,未完待续。。。

原文地址:https://www.cnblogs.com/nklzj/p/6890725.html