【转】python __file__ 与argv[0]

本文详细对比了Python中获取脚本路径的两种方式:__file__和sys.argv[0],解释了它们在不同场景下的表现,并讨论了为何argv[0]在多数情况下更为可靠。

原文地址:http://www.4ucode.com/Study/Topic/1477794

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/MonkeyD-Lufy/archive/2012/02/20/2360235.html

设置 Python 的 `FILE_PATH` 通常有以下几种方式: ### 环境变量方式 可以在系统中设置环境变量 `FILE_PATH`,Python 程序可以通过 `os.getenv()` 来获取该环境变量的值。 #### 在 Linux 或 macOS 系统中 在终端中可以使用 `export` 命令临时设置环境变量,或者将其添加到 `.bashrc` 或 `.zshrc` 文件中实现永久设置。 临时设置: ```bash export FILE_PATH="/path/to/your/file" ``` 永久设置: ```bash echo 'export FILE_PATH="/path/to/your/file"' >> ~/.bashrc source ~/.bashrc ``` 在 Python 中获取该环境变量的值: ```python import os file_path = os.getenv("FILE_PATH") if file_path: print(f"获取到的文件路径是: {file_path}") else: print("未设置 FILE_PATH 环境变量") ``` #### 在 Windows 系统中 可以通过命令行或者系统设置来设置环境变量。 命令行临时设置: ```powershell $env:FILE_PATH = "C:\path\to\your\file" ``` 通过系统设置永久设置: 1. 搜索并打开“编辑系统环境变量”。 2. 在“系统变量”中点击“新建”,变量名输入 `FILE_PATH`,变量值输入文件路径。 3. 点击“确定”保存设置。 在 Python 中获取该环境变量的值的代码 Linux/macOS 系统相同。 ### 脚本内硬编码 在 Python 脚本中直接定义 `FILE_PATH` 变量。 ```python FILE_PATH = "/path/to/your/file" print(f"文件路径是: {FILE_PATH}") ``` ### 命令行参数方式 在运行 Python 脚本时,通过命令行传递文件路径作为参数,在脚本中使用 `sys.argv` 来获取。 ```python import sys if len(sys.argv) > 1: file_path = sys.argv[1] print(f"获取到的文件路径是: {file_path}") else: print("请在运行脚本时提供文件路径作为参数") ``` 运行脚本时的示例: ```bash python your_script.py /path/to/your/file ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值