I have written a small Windows Service compiled to an exe using py2exe, which from I read a file. It works great - however - I can only use an absolute path to access the file, using a relative path won't work. In .net you can do something like:
System.Reflection.Assembly.GetEntryAssembly().Location
To get the path to the .exe file, is there an option similar to this but for Python?
Best Regards,
Anders
You have a couple of options. If you already have a module object, you can do <module>.__file__
to get the path:
>>> import time
>>> time.__file__
'/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/lib-dynload/time.so'
However, often times you want to know the location of your current module, in which case you can use:
import inspect
path = inspect.currentframe().f_code.co_filename
(Also keep in mind that this can be anything that can be loaded by Python - so it can be a .pyd, .pyc, .so, .dll, etc.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.