python怎么获取当前执行文件的绝对路径
时间: 2024-04-30 08:23:52 浏览: 18
可以使用以下代码来获取当前执行文件的绝对路径:
```python
import os
current_path = os.path.abspath(__file__)
print(current_path)
```
其中,`os.path.abspath(__file__)`可以获取当前文件的绝对路径,`print()`函数用来打印输出结果。
相关问题
python 获取执行文件当前路径
可以使用Python内置的os模块来获取当前执行文件的路径。
代码如下:
```python
import os
# 获取当前执行文件的绝对路径
current_path = os.path.abspath(__file__)
print(current_path)
# 获取当前执行文件所在的目录路径
current_dir = os.path.dirname(current_path)
print(current_dir)
```
其中,`os.path.abspath()`函数可以获取当前执行文件的绝对路径,而`os.path.dirname()`函数可以获取当前执行文件所在的目录路径。在这个例子中,我们先获取当前执行文件的绝对路径,然后再获取其所在目录的路径。
python获取当前文件路径
可以使用Python内置的os模块中的os.path.realpath()函数来获取当前文件的绝对路径,示例代码如下:
```python
import os
current_path = os.path.realpath(__file__)
print(current_path)
```
其中,\_\_file\_\_是一个内置变量,代表当前执行的脚本文件路径。`os.path.realpath()`函数可以将相对路径转换为绝对路径,因此可以通过该函数获取当前文件的绝对路径。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)