Python 獲取當前路徑幾種方法

Python 獲取當前路徑的幾種方法


絕對路徑

1、os.path 方法

# -*- coding: utf-8 -*-
# !/usr/bin/python

import os
import sys

current_directory = os.path.dirname(os.path.abspath(__file__))

print(current_directory)


輸出:
Alt

2、os.path.abspath 方法

# -*- coding: utf-8 -*-
# !/usr/bin/python

import os
import sys

root_path = os.path.abspath(os.path.dirname(current_directory) + os.path.sep + ".")
sys.path.append(root_path)

print(sys.path[0])

輸出:
Alt

3、os.getcwd 方法

currentPath = os.getcwd().replace('\\','/')    # 獲取當前路徑

print(currentPath)

輸出:
Alt


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章