Python 获取当前路径的几种方法
绝对路径
1、os.path
方法
# -*- coding: utf-8 -*-# !/usr/bin/pythonimport osimport syscurrent_directory = os.path.dirname(os.path.abspath(__file__))print(current_directory)
输出:
2、os.path.abspath
方法
# -*- coding: utf-8 -*-# !/usr/bin/pythonimport osimport sysroot_path = os.path.abspath(os.path.dirname(current_directory) + os.path.sep + ".")sys.path.append(root_path)print(sys.path[0])
输出:
3、os.getcwd
方法
currentPath = os.getcwd().replace('\\','/') # 获取当前路径print(currentPath)
输出: