Python 获取当前路径3种方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Python 获取当前路径3种方法

大小宝   2023-02-03 我要评论

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

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们