Python 获取当前路径3种方法
作者:大小宝 时间:2023-04-15 00:03:24
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)
输出:
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])
输出:
3、os.getcwd
方法
currentPath = os.getcwd().replace('\\','/') # 获取当前路径
print(currentPath)
输出:
来源:https://blog.csdn.net/hongzhen91/article/details/103274923
标签:Python,当前路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
SQL Server中读取XML文件的简单做法
2008-12-23 15:29:00
SQL Server全文检索查询浅析
2024-01-15 22:58:38
php验证码的制作思路和实现方法
2023-09-04 13:23:37
![](https://img.aspxhome.com/file/2023/5/55505_0s.jpg)
js保留两位小数方法总结
2023-07-18 17:34:35
Python实战之实现获取动态图表
2023-10-31 14:03:34
![](https://img.aspxhome.com/file/2023/3/97703_0s.jpg)
python执行子进程实现进程间通信的方法
2021-04-23 04:29:20
python中如何使用虚拟环境
2021-02-17 22:07:49
![](https://img.aspxhome.com/file/2023/6/121056_0s.png)
python多线程http压力测试脚本
2022-12-31 16:48:37
go语言实现简易比特币系统钱包的原理解析
2024-05-22 10:12:02
使用FormData实现上传多个文件
2024-05-11 09:30:20
![](https://img.aspxhome.com/file/2023/9/126179_0s.png)
PHP操作MySQL中BLOB字段的方法示例【存储文本与图片】
2023-11-23 23:45:27
Django3基于WebSocket实现WebShell的详细过程
2021-12-01 21:34:31
表单设计中的网页视觉体验
2008-06-26 13:35:00
![](https://img.aspxhome.com/file/UploadPic/20086/26/2008626134221567s.jpg)
Python函数的迭代器与生成器的示例代码
2022-09-14 01:29:58
![](https://img.aspxhome.com/file/2023/4/121494_0s.png)
Django多进程滚动日志问题解决方案
2023-02-01 16:21:16
python 利用zmail库发送邮件
2023-02-25 11:06:03
![](https://img.aspxhome.com/file/2023/4/118494_0s.png)
form 元素内的字段 name 不要跟 form 属性名称一致
2008-10-22 13:25:00
python 用opencv实现霍夫线变换
2021-09-14 20:18:25
![](https://img.aspxhome.com/file/2023/3/124943_0s.png)
vue+vux实现移动端文件上传样式
2024-05-02 16:34:40
![](https://img.aspxhome.com/file/2023/2/130232_0s.png)
uniapp H5 https跨域请求实现
2024-04-10 16:20:27
![](https://img.aspxhome.com/file/2023/0/136700_0s.png)