Python 获取当前路径3种方法

本文主要介绍了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

到此这篇关于Python 获取当前路径3种方法的文章就介绍到这了,更多相关Python 获取当前路径内容请搜索0133技术站以前的文章或继续浏览下面的相关文章希望大家以后多多支持0133技术站!

以上就是Python 获取当前路径3种方法的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » python