python获取当前路径

栏目: Python · 发布时间: 7年前

内容简介:总结一下python获取目录的方法在/usr/local/scripts 目录下创建一个basedir.py,具体内容如下在/usr/local/scripts/mysql 目录下执行python basedir.py,结果如下

总结一下 python 获取目录的方法

在/usr/local/scripts 目录下创建一个basedir.py,具体内容如下

# -*- encoding=utf-8 -*-

import os, sys 

print("__file__ = %s" % __file__) 
print("os.path.realpath(__file__) = %s" % os.path.realpath(__file__))
print("os.path.dirname(os.path.realpath(__file__)) = %s" % os.path.dirname(os.path.realpath(__file__)))
print("os.path.split(os.path.realpath(__file__)) = %s" % os.path.split(os.path.realpath(__file__))[0])
print("os.path.abspath(__file__) = %s" % os.path.abspath(__file__))
print("os.path[0] = %s" % sys.path[0])
print("os.getcwd() = %s" % os.getcwd())
print("sys.argv[0] = %s" % sys.argv[0])

在/usr/local/scripts/mysql 目录下执行python basedir.py,结果如下

__file__ = basedir.py
os.path.realpath(__file__) = /usr/local/scripts/mysql/basedir.py
os.path.dirname(os.path.realpath(__file__)) = /usr/local/scripts/mysql
os.path.split(os.path.realpath(__file__)) = /usr/local/scripts/mysql
os.path.abspath(__file__) = /usr/local/scripts/mysql/basedir.py
os.path[0] = /usr/local/scripts/mysql
os.getcwd() = /usr/local/scripts/mysql
sys.argv[0] = basedir.py

在root目录下执行python /usr/local/scripts/mysql/basedir.py,结果如下

__file__ = /usr/local/scripts/mysql/basedir.py
os.path.realpath(__file__) = /usr/local/scripts/mysql/basedir.py
os.path.dirname(os.path.realpath(__file__)) = /usr/local/scripts/mysql
os.path.split(os.path.realpath(__file__)) = /usr/local/scripts/mysql
os.path.abspath(__file__) = /usr/local/scripts/mysql/basedir.py
os.path[0] = /usr/local/scripts/mysql
os.getcwd() = /root
sys.argv[0] = /usr/local/scripts/mysql/basedir.py

os.path[0] 获得的是脚本文件的所在目录

os.getcwd() 获得的是执行脚本时是在的目录


以上所述就是小编给大家介绍的《python获取当前路径》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

The Sovereign Individual

The Sovereign Individual

James Dale Davidson、William Rees-Mogg / Free Press / 1999-08-26 / USD 16.00

Two renowned investment advisors and authors of the bestseller The Great Reckoning bring to light both currents of disaster and the potential for prosperity and renewal in the face of radical changes ......一起来看看 《The Sovereign Individual》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

URL 编码/解码
URL 编码/解码

URL 编码/解码

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试