python 获取脚本所在目录
平时写python经常会想获得脚本所在的目录,例如有个文件跟脚本文件放在一个相对的目录位置,那就可以通过脚本文件的目录找到对应的文件,即使以后脚本文件移到其他地方,脚本也基本不需要改动(相对于写死目录的好处)。下面通过一些代码进行一下对比。
这是我写的一段代码在:/root/printfabcd/py/filePath.py
- 20 logger.debug("sys.path:"+sys.path[0])
- 21 logger.debug("sys.argv:"+sys.argv[0])
- 22 logger.debug("__file__:"+__file__)
- 23 logger.debug("os.getcwd():"+os.getcwd())
- 24 logger.debug("os.path.realpath():"+os.path.realpath(__file__))
- 25 logger.debug("(os.path.abspath(__file__)):"+os.path.abspath(__file__))
- 26 logger.debug("os.path.dirname(os.path.realpath()):"+os.path.dirname(os.path.realpath(__file__)))
- 27 logger.debug("os.path.dirname(os.path.abspath(__file__)):"+os.path.dirname(os.path.abspath(__file__)))
在/root/printfabcd/py 目录下执行:python filePath.py
- 2011-11-27 13:20:20,090 DEBUG filePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:20,090 DEBUG filePath 21 sys.argv:filePath.py
- 2011-11-27 13:20:20,090 DEBUG filePath 22 __file__:filePath.py
- 2011-11-27 13:20:20,090 DEBUG filePath 23 os.getcwd():/root/printfabcd/py
- 2011-11-27 13:20:20,091 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:20,091 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:20,091 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 2011-11-27 13:20:20,091 DEBUG filePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py
在/root 目录下执行:python printfabcd/py/filePath.py
2
- 2011-11-27 13:20:39,227 DEBUG filePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:39,227 DEBUG filePath 21 sys.argv:printfabcd/py/filePath.py
- 2011-11-27 13:20:39,227 DEBUG filePath 22 __file__:printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 23 os.getcwd():/root
- 2011-11-27 13:20:39,228 DEBUG filePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:39,228 DEBUG filePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 2011-11-27 13:20:39,228 DEBUG filePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py
从上面两个输出可以看出来,如果想获得脚本所在目录最简单就是通过sys.path[0],os.getcwd()获得的是执行脚本的目录。
下面在做一下测试 我在/root/printfabcd/py/下创建一个软连接tfilePath.py 指向filePath.py,执行tfilePath.py
- 2011-11-27 13:20:57,466 DEBUG tfilePath 20 sys.path:/root/printfabcd/py
- 2011-11-27 13:20:57,466 DEBUG tfilePath 21 sys.argv:printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,466 DEBUG tfilePath 22 __file__:printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 23 os.getcwd():/root
- 2011-11-27 13:20:57,467 DEBUG tfilePath 24 os.path.realpath():/root/printfabcd/py/filePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 25 (os.path.abspath(__file__)):/root/printfabcd/py/tfilePath.py
- 2011-11-27 13:20:57,467 DEBUG tfilePath 26 os.path.dirname(os.path.realpath()):/root/printfabcd/py
- 2011-11-27 13:20:57,468 DEBUG tfilePath 27 os.path.dirname(os.path.abspath(__file__)):/root/printfabcd/py
仔细看os.path.realpath()和os.path.abspath()可以看出他们的区别,即使你创建了软连接,realpath还是可以拿到真正对应的文件。我是一个python的初学者懂得不多,如果发现什么问题,请多多指正。
下面是__file__与sys.argv[0]的一个对比
转载自:http://andylin02.iteye.com/blog/933237
python __file__ 与argv[0]
在python下,获取当前执行主脚本的方法有两个:sys.argv[0]和__file__。
sys.argv[0]
获取主执行文件路径的最佳方法是用sys.argv[0],它可能是一个相对路径,所以再取一下abspath是保险的做法,像这样:
import os,sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
print "running from", dirname
print "file is", filename
__file__
__file__ 是用来获得模块所在的路径的,这可能得到的是一个相对路径,比如在脚本test.py中写入:
#!/usr/bin/env python
print __file__
- 按相对路径./test.py来执行,则打印得到的是相对路径,
- 按绝对路径执行则得到的是绝对路径。
- 而按用户目录来执行(~/practice/test.py),则得到的也是绝对路径(~被展开)
- 所以为了得到绝对路径,我们需要 os.path.realpath(__file__)。
而在Python控制台下,直接使用print __file__是会导致 name ‘__file__’ is not defined错误的,因为这时没有在任何一个脚本下执行,自然没有 __file__的定义了。
__file__和argv[0]差异
在主执行文件中时,两者没什么差异,不过要是在不同的文件下,就不同了,下面示例:
C:\junk\so>type \junk\so\scriptpath\script1.py
import sys, os
print "script: sys.argv[0] is", repr(sys.argv[0])
print "script: __file__ is", repr(__file__)
print "script: cwd is", repr(os.getcwd())
import whereutils
whereutils.show_where()C:\junk\so>type \python26\lib\site-packages\whereutils.py
import sys, os
def show_where():print "show_where: sys.argv[0] is", repr(sys.argv[0])print "show_where: __file__ is", repr(__file__)print "show_where: cwd is", repr(os.getcwd())C:\junk\so>\python26\python scriptpath\script1.py
script: sys.argv[0] is 'scriptpath\\script1.py'
script: __file__ is 'scriptpath\\script1.py'
script: cwd is 'C:\\junk\\so'
show_where: sys.argv[0] is 'scriptpath\\script1.py'
show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc'
show_where: cwd is 'C:\\junk\\so'
所以一般来说,argv[0]要更可靠些。
转载于:https://www.cnblogs.com/sthinker/p/7456048.html
python 获取脚本所在目录相关推荐
- python获取当前路径的方法_Python获取脚本所在目录的正确方法【转】
原博文 2015-09-24 10:21 − 1.以前的方法如果是要获得程序运行的当前目录所在位置,那么可以使用os模块的os.getcwd()函数.如果是要获得当前执行的脚本的所在目录位置,那么需要 ...
- python 获取脚本当前路径_Python获取脚本所在目录的正确方法
http://www.jb51.net/article/49035.htm 文章主要介绍了Python获取脚本所在目录的正确方法 1.以前的方法如果是要获得程序运行的当前目录所在位置,那么可以使用os ...
- python 脚本所在目录,Python 获取当前所在目录的方法详解
sys.path 模块搜索路径的字符串列表.由环境变量PYTHONPATH初始化得到. sys.path[0]是调用Python解释器的当前脚本所在的目录. sys.argv 一个传给Python脚本 ...
- shell脚本中获取当前所在目录地址
shell脚本中获取当前所在目录如下 #!/bin/bashwork_path=$(dirname $0) cd ${work_path} work_path=$(pwd) cd ${work_pat ...
- shell 获取运行脚本所在目录
可以使用以下命令来获取shell脚本所在目录: #!/bin/bash # 获取当前脚本所在目录 current_dir=$(cd $(dirname $0); pwd) echo $current_ ...
- linux shell获取当前脚本所在目录
1 . 获得shell脚本所在的绝对路径 CURRENT_DIR=$(cd $(dirname $0); pwd) 或者 CURRENT_DIR=$(cd `dirname $0`; pwd) 执行步 ...
- Python - 获取当前目录/上级目录/上上级目录
/ 前言 / 我们在Python开发中时常需要读取配置文件.获取当前文件所在路径, 本文将会介绍Python获取当前目录/上级目录/上上级目录-等操作 Python脚本编译时使用的是Py ...
- php 获取key的位置,PHP获取当前所在目录位置的方法
本文实例讲述了PHP获取当前所在目录位置的方法.分享给大家供大家参考.具体分析如下: 如果要获取脚本文件的目录,要应用函数getcwd()来实现.函数声明如下: string getcwd ( voi ...
- linux存放脚本目录,linux中Shell脚本所在目录的绝对路径linux操作系统 -电脑资料...
linux shell 脚本里面如果想得到当前脚本文件存放的绝对路径,也没有太现成的命令可以调用,不过可以通过下面的语句来获取:代码如下复制代码 baseDirForScriptSelf=$(cd & ...
最新文章
- EdgeBERT:极限压缩bert
- optee对std smc的处理的详解
- 光线求交加速算法:边界体积层次结构(Bounding Volume Hierarchies)3-LBVH(Linear Bounding Volume Hierarchies)
- 初探 amaze-vue( 基于vue.js封装的Amaze UI 组件库)
- linux断点续传程序,Linux中实现断点续传的原理
- python数字转中文字符_python数字金额转换为中文大写金额
- 闪迪u盘不能识别好办法_闪迪u盘修复工具,小编教你怎么修复闪迪U盘
- 中国智能燃气表行业发展态势分析及投资风险评估报告2022-2028年版
- java编程马拉松比赛_腾讯编程马拉松
- [积水成渊]asp.net中HiddenField的使用
- 针对C64x+的一些优化经验(转帖)
- 五笔字型输入规则-温故而知新
- html病毒实验,计算机病毒实验手册4-5-9-10-8-v4.0.pdf
- 自然语言处理在eBay的技术实践
- Linux jar包 后台运行命令
- 如何在WordPress中为用户设置自定义头像(Gravatar替代)
- mysql 存储过程 队列_mysql使用存储过程函数实现批量插入
- 鸿蒙系统重大缺陷,华为鸿蒙6月份规模化推送?部分用户已经体验,全新的简洁风...
- 昆山第二中等专业学校计算机分数线,江苏昆山第一中等专业学校2021年招生录取分数线...
- IM Terminator?
热门文章
- 网络名词--“环路”
- robots.txt文件的解析及过滤
- JAVA Functions in XI(转)
- 一篇文章让你了解区块链技术的发展阶段
- github创建静态页面_如何在10分钟内使用GitHub Pages创建免费的静态站点
- aws 认证_引入#AWSCertified挑战:您的第一个AWS认证之路
- bigquery使用教程_如何使用Python和Google BigQuery构建机器人以自动执行您的笨拙任务...
- mac下的intellij idea常用快捷键
- bat 将war文件转换成ear文件
- mysql启动与关闭(手动与自动)