- 1.1、路径获取
- 1.2、路径更改
- 1.3、目录遍历
- 当前文件路径
A:python file_path = sys.argv[0]
B:python file_path = os.path.realpath(__file__)
C:python pwd_path = os.path.abspath($v_filename)
说明:以上几种获取当前文件路径的方法,结果包含 文件本身;
- 当前文件目录
A:python dir_path= os.path.dirname(file_path)
说明:在获取到 文件路径基础上,获得 当前文件目录;
B:python pwd_path = os.getcwd()
C:python pwd_path = os.path.abspath(".")
D:python pwd_path = os.path.abspath(os.curdir)
- 获取上级路径
parent_path = os.path.abspath("..")
parent_path = os.path.abspath("...")
# 获取上 2级目录
parent_path = os.path.abspath("....")
- 路径拼接
file_path = os.path.join(path_1,path_2,...path_n)
说明:路径拼接函数的参数个数问不定长参数;
1.2、路径更改os.chdir($target_path)1.3、目录遍历
os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]])
代码实现
import os.path
start_path = os.path.abspath("..")
print(start_path)
for startNode,dirs,v_file in os.walk(start_path):
print(startNode)
print(dirs)
print(v_file)



