python 获取脚本所在目录

本文对比了多种获取Python脚本路径的方法,包括sys.argv[0]、__file__及os模块函数等,并分析了它们在不同场景下的表现,为开发者提供了一份实用指南。

平时写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]要更可靠些。

 

 

 

 

Python 中,获取**当前脚本所在的目录路径**是一个常见需求,尤其是在需要读取同级或子目录中的配置文件、数据文件时。正确获取脚本所在目录可以避免因工作目录不同导致的路径错误。 --- ### ✅ 方法一:使用 `pathlib`(推荐,Python 3.4+) ```python from pathlib import Path # 获取当前脚本的文件路径 script_path = Path(__file__).resolve() print("脚本完整路径:", script_path) # 获取脚本所在目录 script_dir = script_path.parent print("脚本所在目录:", script_dir) # 示例:读取同目录下的 data.txt data_file = script_dir / 'data.txt' if data_file.exists(): content = data_file.read_text(encoding='utf-8') print(content) else: print("文件不存在") ``` > 💡 `Path(__file__)` 表示当前脚本的路径;`.resolve()` 确保解析符号链接和相对路径;`.parent` 获取目录。 --- ### ✅ 方法二:使用 `os.path` ```python import os # 获取当前脚本的绝对路径 script_path = os.path.realpath(__file__) print("脚本完整路径:", script_path) # 获取脚本所在目录 script_dir = os.path.dirname(script_path) print("脚本所在目录:", script_dir) # 示例:拼接文件路径 data_path = os.path.join(script_dir, 'data.txt') if os.path.exists(data_path): with open(data_path, 'r', encoding='utf-8') as f: print(f.read()) else: print("文件不存在") ``` > 🔍 `os.path.realpath(__file__)` 返回脚本的真实绝对路径(处理了符号链接),比 `__file__` 更可靠。 --- ### ⚠️ 常见误区:不要用 `os.getcwd()` ```python import os print(os.getcwd()) # ❌ 错误!这是“当前工作目录”,不是脚本所在目录 ``` - `os.getcwd()` 返回的是程序启动时的**工作目录**,可能与脚本位置完全不同。 - 比如你在桌面运行 `python D:\project\main.py`,那么 `getcwd()` 是桌面路径,而不是 `D:\project`。 --- ### 🧩 实际应用场景 假设项目结构如下: ``` /my_project ├── main.py └── config/ └── settings.json ``` 你想在 `main.py` 中读取 `config/settings.json`,应该这样做: ```python from pathlib import Path import json # 获取脚本所在目录 SCRIPT_DIR = Path(__file__).parent # 构建配置文件路径 config_path = SCRIPT_DIR / 'config' / 'settings.json' # 安全读取 try: with open(config_path, 'r', encoding='utf-8') as f: settings = json.load(f) print("配置加载成功:", settings) except FileNotFoundError: print(f"配置文件未找到:{config_path}") ``` --- ### 🔐 特殊情况说明 | 场景 | 是否可用 `__file__` | 替代方案 | |------|---------------------|----------| | 普通 `.py` 脚本 | ✅ 可用 | 无 | | Jupyter Notebook | ❌ 不适用 | 使用 `os.getcwd()` 或 `%pwd` | | 打包成 exe(PyInstaller) | ❌ `__file__` 失效 | 使用 `sys._MEIPASS` | #### 打包环境下的兼容写法(PyInstaller 支持): ```python import sys from pathlib import Path if getattr(sys, 'frozen', False): # 打包后的可执行文件 script_dir = Path(sys.executable).parent else: # 正常 Python 脚本 script_dir = Path(__file__).parent print("脚本目录(兼容模式):", script_dir) ``` --- ### 总结 | 方法 | 推荐度 | 说明 | |------|--------|------| | `Path(__file__).parent` | ✅✅✅ 强烈推荐 | 现代、简洁、跨平台 | | `os.path.dirname(os.path.realpath(__file__))` | ✅✅ 推荐 | 兼容老版本 Python | | `os.getcwd()` | ❌ 不推荐 | 不是脚本目录,容易出错 | ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值