How do I get the path and name of the file that is currently executing?

后端 未结 29 2373
你的背包
你的背包 2020-11-22 06:43

I have scripts calling other script files but I need to get the filepath of the file that is currently running within the process.

For example, let\'s say I have th

相关标签:
29条回答
  • 2020-11-22 07:16
    import os
    
    import wx
    
    
    # return the full path of this file
    print(os.getcwd())
    
    icon = wx.Icon(os.getcwd() + '/img/image.png', wx.BITMAP_TYPE_PNG, 16, 16)
    
    # put the icon on the frame
    self.SetIcon(icon)
    
    0 讨论(0)
  • 2020-11-22 07:17

    I wrote a function which take into account eclipse debugger and unittest. It return the folder of the first script you launch. You can optionally specify the __file__ var, but the main thing is that you don't have to share this variable across all your calling hierarchy.

    Maybe you can handle others stack particular cases I didn't see, but for me it's ok.

    import inspect, os
    def getRootDirectory(_file_=None):
        """
        Get the directory of the root execution file
        Can help: http://stackoverflow.com/questions/50499/how-do-i-get-the-path-and-name-of-the-file-that-is-currently-executing
        For eclipse user with unittest or debugger, the function search for the correct folder in the stack
        You can pass __file__ (with 4 underscores) if you want the caller directory
        """
        # If we don't have the __file__ :
        if _file_ is None:
            # We get the last :
            rootFile = inspect.stack()[-1][1]
            folder = os.path.abspath(rootFile)
            # If we use unittest :
            if ("/pysrc" in folder) & ("org.python.pydev" in folder):
                previous = None
                # We search from left to right the case.py :
                for el in inspect.stack():
                    currentFile = os.path.abspath(el[1])
                    if ("unittest/case.py" in currentFile) | ("org.python.pydev" in currentFile):
                        break
                    previous = currentFile
                folder = previous
            # We return the folder :
            return os.path.dirname(folder)
        else:
            # We return the folder according to specified __file__ :
            return os.path.dirname(os.path.realpath(_file_))
    
    0 讨论(0)
  • 2020-11-22 07:18

    The suggestions marked as best are all true if your script consists of only one file.

    If you want to find out the name of the executable (i.e. the root file passed to the python interpreter for the current program) from a file that may be imported as a module, you need to do this (let's assume this is in a file named foo.py):

    import inspect

    print inspect.stack()[-1][1]

    Because the last thing ([-1]) on the stack is the first thing that went into it (stacks are LIFO/FILO data structures).

    Then in file bar.py if you import foo it'll print bar.py, rather than foo.py, which would be the value of all of these:

    • __file__
    • inspect.getfile(inspect.currentframe())
    • inspect.stack()[0][1]
    0 讨论(0)
  • 2020-11-22 07:18
    import sys
    print sys.argv[0]
    
    0 讨论(0)
  • 2020-11-22 07:18

    I used the approach with __file__
    os.path.abspath(__file__)
    but there is a little trick, it returns the .py file when the code is run the first time, next runs give the name of *.pyc file
    so I stayed with:
    inspect.getfile(inspect.currentframe())
    or
    sys._getframe().f_code.co_filename

    0 讨论(0)
提交回复
热议问题