栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

查找当前运行文件的路径

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

查找当前运行文件的路径

__file__
不是您要找的东西。不要使用意外的副作用

sys.argv[0]
永远 的脚本路径(如果事实上脚本已经调用)
-见http://docs.python.org/library/sys.html#sys.argv

__file__
当前正在执行的 文件(脚本或模块)的路径。这是 偶然
一样的,如果它是从脚本访问的脚本!如果要将诸如相对于脚本位置的资源文件定位到库中的有用操作,则必须使用
sys.argv[0]

例:

C:junkso>type junksoscriptpathscript1.pyimport sys, osprint "script: sys.argv[0] is", repr(sys.argv[0])print "script: __file__ is", repr(__file__)print "script: cwd is", repr(os.getcwd())import whereutilswhereutils.show_where()C:junkso>type python26libsite-packageswhereutils.pyimport sys, osdef 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:junkso>python26python scriptpathscript1.pyscript: 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'


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633751.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号