Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I recently downlaoded a game on pydev.org. The code below is the code that you run for the game to play. My question is does the likes of
os.path.dirname(__file__)
mean? I have created games in python before but i always made them in the one class. Where as in this game this is outside of the folder where all the other classes are?

Can any1 help me. I have researched this but to no luck!
import sys
import os


try:
    __file__
except NameError:
    pass
else:
    libdir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'lib'))
    sys.path.insert(0, libdir)

sys.path.insert(1, "lib")
import main
main.main()
Posted

1 solution

os.path.dirname(path)[^] is a function that will return the path to a file where the parameter given is the complete path including the file and an optional extension.
__file__ (see here for instance: http://docs.python.org/library/runpy.html[^]) is a special global variable that will give the file name of the file that contains the momentarily executed code. So if you use this variable in your code it will give you the file name that contains the instructions you wrote.

So the construct in question will give you the path only of the file containing the momentarily executed instructions.

Regards,

Manfred
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900