1、os.path模块
# os.path常用方法
from os import path p=path.join(‘/etc‘,‘sysconfig‘,‘network‘) # 路径拼接 print(type(p),p) print(path.exists(p)) # 判断路径是否存在 print(path.split(p)) # 将路径切割为2部分,head,tail print(path.abspath(‘.‘)) # 当前路径的绝对路径 p=path.join(‘o:/‘,p,‘test.txt‘) print(p) # o:/etc\sysconfig\network\test.txt print(path.dirname(p)) # o:/etc\sysconfig\network print(path.basename(p)) # test.txt p1=path.abspath(__file__) while p1 != path.dirname(p1): p1=path.dirname(p1) print(p1,path.basename(p1)) ‘‘‘ 运行结果: F:\study-python\com.lx.study\week7 week7 F:\study-python\com.lx.study com.lx.study F:\study-python study-python F:\ ‘‘‘
2、pathlib模块
# python3.4版本引入pathlib模块,提供Path对象来操作,包括目录和文件;
原文地址:https://www.cnblogs.com/soulgou123/p/9818779.html
时间: 2024-11-08 15:48:44