python获取当前执行命令的路径:
#!/usr/bin/env python
# -*# coding: utf-8 -*-
import os
print os.getcwd()
python获取当前文件所在的路径:
#!/usr/bin/env python
# -*# coding: utf-8 -*-
import sys
print sys.path[0]
假设我有一个文件为 /usr/test.py
, test.py
里面的内容是:
#!/usr/bin/env python
# -*# coding: utf-8 -*-
import os
import sys
print os.getcwd()
print sys.path[0]
我在 /etc/
中运行,那么命令是:
[root@localhost /]# python /usr/test.py
此时打印出来的结果是:
os.getcwd() = /
sys.path[0] = /usr
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有