1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python 获取当前路径几种方法

Python 获取当前路径几种方法

时间:2024-02-08 02:55:52

相关推荐

Python 获取当前路径几种方法

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)

输出:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。