python __file__ 与argv[0]
来源:互联网 发布:兼职网络鉴黄师招聘 编辑:程序博客网 时间:2024/05/20 02:29
python __file__ 与argv[0]
在python下,获取当前执行主脚本的方法有两个:sys.argv[0]和__file__。
sys.argv[0]
获取主执行文件路径的最佳方法是用sys.argv[0],他就是脚本的第一个参数,所以它的值在本模块以及被调用模块中都保持不变,它可能是一个相对路径,输入的是绝对路径就是绝对路径,输入的是相对路径就是相对路径。所以再取一下abspath是保险的做法,像这样:
import os,sysdirname, 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__的定义了。
os.getcwd()函数是获得当前的工作目录,与该模块在哪个目录无关。即显示pwd的内容。
__file__和argv[0]差异
在主执行文件中时,两者没什么差异,不过要是在不同的文件下,就不同了,下面示例:
Z:\mycode\a.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())
print "show_where: realpath is",os.path.realpath(__file__)
from c import b
b.show_where()
Z:\mycode\c\b.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())
print "show_where: realpath is",os.path.realpath(__file__)
-bash-4.1$ python a.py
script: sys.argv[0] is 'a.py'
script: __file__ is 'a.py'
script: cwd is '/dbc/pek2-dbc201/danyangw/mycode'
script: realpath is /dbc/pek2-dbc201/danyangw/mycode/a.py
show_where: sys.argv[0] is 'a.py'
show_where: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/c/b.pyc'
show_where: cwd is '/dbc/pek2-dbc201/danyangw/mycode'
show_where: realpath is /dbc/pek2-dbc201/danyangw/mycode/c/b.pyc
若绝对路径执行则:
-bash-4.1$ python /dbc/pek2-dbc201/danyangw/mycode/a.py
script: sys.argv[0] is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
script: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
script: cwd is '/dbc/pek2-dbc201/danyangw'
script: realpath is /dbc/pek2-dbc201/danyangw/mycode/a.py
show_where: sys.argv[0] is '/dbc/pek2-dbc201/danyangw/mycode/a.py'
show_where: __file__ is '/dbc/pek2-dbc201/danyangw/mycode/c/b.py'
show_where: cwd is '/dbc/pek2-dbc201/danyangw'
show_where: realpath is /dbc/pek2-dbc201/danyangw/mycode/c/b.py
所以一般来说,argv[0]要更可靠些。
- python __file__ 与argv[0]
- python __file__ 与argv[0]
- python __file__ 与argv[0]
- python __file__ 与argv[0]
- python __file__ 与argv[0]
- __file__ 与 argv[0]
- python中关于__file__ 和 sys.argv[0]的使用
- Python基础-__name和__file__和argv[0]
- python __init__.py __name__ __doc__ __file__ argv[0] 浅析
- Python获取当前文件名分两种方法:__file__、sys.argv[0]
- python __file__ 与相对路径
- python的argc与argv
- [转]python __file__ 与相对路径
- python:9:os模块与sys.argv
- python argv
- 关于(*++argv)[0]与argc
- python __file__的绝对路径与import的搜索路径
- *++argv *++argv[0]
- 如何查看OpenCV源码
- 【转】我刚做的一个TreeView的CheckBox进行选中插入数据库,从数据库中读取数据后让CheckBox勾选的代码!
- JMS request-response implementation
- Android开源二维码识别项目zxing横屏改为竖屏识别解决方案
- 分享4台微软云Azure虚拟机
- python __file__ 与argv[0]
- 黑马程序员——OC三大特性(继承、多态、封装)
- oracle DML错误日志
- cvbs s-video vga dvi hdmi
- Windows 7驱动自动安装设置及手动更新方法
- T-SQL 判断 是否存在(数据库,表,存储过程,约束。。。和列)
- 算法题目------【一个桶内100个白球,100个黑球,最后一个是黑球的概率是多少?】
- zookeeper原理
- Problem 1: Two Sum