问题
I have a python program, and I want to get the path to the program from within the program, but INCLUDING the file name itself. The name of my file is PyWrapper.py. Right now I'm doing this:
import sys,os
pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
print fullpath
The output is:
/home/mrpickles/Desktop/HANSTUFF/securesdk/src/
This is the path to the directory in which my file is saved, but I would like it to output:
/home/mrpickles/Desktop/HANSTUFF/securesk/src/PyWrapper.py/
Which is the path, including the filename itself. Is this possible? Thanks.
回答1:
Take a look at __file__
. This gives you a filename where the code actually is.
Also, there's another option:
import __main__ as main
print(main.__file__)
This gives you a filename of a script being run (similar to what your argv
does).
The difference comes into play when the code is imported by another script.
回答2:
print __file__
should work.
__file__
returns the path of the executed file
回答3:
Just in case you would like to find the absolute path for other files, not just the one you are currently running, in that same directory, a general approach could look like this:
import sys,os
pathname = os.path.dirname(sys.argv[0])
fullpath = os.path.abspath(pathname)
for root, dirs, files in os.walk(fullpath):
for name in files:
name = str(name)
name = os.path.realpath(os.path.join(root,name))
print name
As others are mentioning, you could take advantage of the __file__
attribute. You can use the __file__
attribute to return several different paths relevant to the currently loaded Python module (copied from another StackOverflow answer):
When a module is loaded in Python, file is set to its name. You can then use that with other functions to find the directory that the file is located in.
# The parent directory of the directory where program resides.
print os.path.join(os.path.dirname(__file__), '..')
# The canonicalised (?) directory where the program resides.
print os.path.dirname(os.path.realpath(__file__))
# The absolute path of the directory where the program resides.
print os.path.abspath(os.path.dirname(__file__))
Remember to be wary of where the module you are loading came from. It could affect the contents of the __file__
attribute (copied from Python 3 Data model documentation):
__file__
is the pathname of the file from which the module was loaded, if it was loaded from a file. The__file__
attribute may be missing for certain types of modules, such as C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.
回答4:
try using __file__
. as long as the module is ran from a file, i.e. not code in an editor, __file__
will be the abs path to the module!
print __file__
Or for command line.
def get_my_name():
if __name__ == '__main__':
return os.path.abspath(os.path.join(os.curdir, __file__))
else:
return __file__.replace('.pyc', '.py')
来源:https://stackoverflow.com/questions/37754733/how-to-get-full-path-to-python-program-including-filename-within-the-program