[英]Python: How do i get the name of the current archive?
Is there any way to get the current archive name in Python? 有什么方法可以在Python中获取当前的存档名称吗?
Something like 就像是
EggArchive.egg
EggArchive.egg
Lib
解放
---SomePythonFile.py---SomePythonFile.py
From SomePython.py, is there anyway to fetch the .egg name? 从SomePython.py,总有没有获取.egg名称?
The variable __file__
contains the path to the current python file. 变量
__file__
包含当前python文件的路径。 So If you have a structure: 因此,如果您有一个结构:
.
`- your.egg
`-your_module.py
And in your_module.py
you have a function: 在
your_module.py
您有一个函数:
def func():
print(__file__)
The the code: 代码:
import sys
sys.path.append('/path/to/your.egg')
from your_module import func
func()
Will print out: 将打印出:
/path/to/your.egg/your_module.py
So basically you can manipulate the __file__
variable if you know where relatively your module is inside the egg
file and get the full path to the egg
. 因此,基本上可以操纵
__file__
,如果你知道你比较模块内部变量egg
文件并获得完整路径egg
。
To get the relative path to the egg in the script that's in the egg file you'll have to do: 要在egg文件中的脚本中获取egg的相对路径,您必须执行以下操作:
def rel_to_egg(f):
my_dir = os.path.dirname(os.path.abspath(f))
current = my_dir
while not current.endswith('.egg'):
current = os.path.dirname(current)
return os.path.relpath(current, my_dir)
Now let's say __file__ == '/test/my.egg/some/dir/my_script.py'
: 现在让我们说
__file__ == '/test/my.egg/some/dir/my_script.py'
:
>>> print(rel_to_egg(__file__))
'../..'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.