1

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.

4 Answers4

2

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.

Daerdemandt
  • 2,281
  • 18
  • 19
0
print __file__

should work.

__file__ returns the path of the executed file

SvbZ3r0
  • 638
  • 4
  • 19
  • So what I'm trying to do is get the full pathname including the filename and pass it into another function as an argument. Could I set some variable as equal to __file__? Eg, fullpath = __file__? –  Jun 10 '16 at 18:19
  • Also for some reason the formatting god rid of the two underscores before and after file in the previous comment –  Jun 10 '16 at 18:20
  • the formatting problem is because two underscores usually stand for bold, unless you put them in a code wrapper. – SvbZ3r0 Jun 10 '16 at 18:21
0

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.

Community
  • 1
  • 1
Vladislav Martin
  • 1,504
  • 2
  • 15
  • 34
0

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')
TheLazyScripter
  • 2,541
  • 1
  • 10
  • 19