Python 获取当前路径3种方法

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

作者:大小宝原文地址:https://blog.csdn.net/hongzhen91/article/details/103274923

%s 个评论

要回复文章请先登录注册