tayamontreal.blogg.se

Python get file path
Python get file path




python get file path

Home_dir = os.path.join("/", script_path_list, script_path_list) Script_path_list = os.path.normpath(_file_).split(os.sep) # Obtain the home dir of the user in whose home directory this script resides Return os.path.dirname(os.path.realpath(_file_))įinding the home directory of the path in which your Python script residesĪs an addendum to the other answers already here (and not answering the OP's question, since other answers already do that), if the path to your script is /home/gabriel/GS/dev/eRCaGuy_dotfiles/useful_scripts/cpu_logger.py, and you wish to obtain the home directory part of that path, which is /home/gabriel, you can do this: import os # We return the folder according to specified _file_ : If ("unittest/case.py" in currentFile) | ("" in currentFile): # We search from left to right the case.py : If ("/pysrc" in folder) & ("" in folder): You can pass _file_ (with 4 underscores) if you want the caller directory

python get file path

Get the directory of the root execution fileįor eclipse user with unittest or debugger, the function search for the correct folder in the stack Maybe you can handle others stack particular cases I didn't see, but for me it's ok. 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. It return the folder of the first script you launch. I wrote a function which take into account eclipse debugger and unittest. Print os.path.abspath(inspect.stack()) # C:\filepaths\lib\bar.py Print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))) # C:\filepaths\lib Print os.path.abspath(inspect.getfile(inspect.currentframe())) # C:\filepaths\lib\bar.py

python get file path

Print inspect.getfile(inspect.currentframe()) # lib/bar.py Print os.path.dirname(_file_) # (empty string)

python get file path

Print os.path.dirname(os.path.realpath(sys.argv)) # C:\filepaths Print os.path.dirname(os.path.abspath(_file_)) # C:\filepaths Print os.path.abspath(os.path.split(sys.argv)) # C:\filepaths Print os.path.basename(os.path.realpath(sys.argv)) # main.py Print os.path.abspath(_file_) # C:\filepaths\main.py Print os.path.realpath(_file_) # C:\filepaths\main.py Here's to these being added to sys as functions! Credit to and on the following three files, and running main.py from its folder with python main.py (also tried execfiles with absolute paths and calling from a separate folder).Ĭ:\filepaths\foo.py: execfile('lib/bar.py') Print os.path.dirname(os.path.abspath(inspect.stack())) # C:\filepaths\lib print os.path.abspath(inspect.stack()) # C:\filepaths\lib\bar.py The last two have the shortest syntax, i.e. The stack-based ones are the only ones that seem to give reliable results. Here is an experiment based on the answers in this thread - with Python 2.7.10 on Windows. See also Difference between import and execfile It's simplest to just use import foo and import lib.bar - no _init_.py files needed. | 3 | exec | (wasn't able to obtain it) |įor Python 2, it might be clearer to switch to packages so can use from lib import bar - just add empty _init_.py files to the two folders.įor Python 3, execfile doesn't exist - the nearest alternative is exec(open().read()), though this affects the stack frames. | 2 | execfile | os.path.abspath(inspect.stack()) | Lib/bar.py - prints filepath expressions | Python | Run statement | Filepath expression | Here is a summary of experiments with Python 2 and 3.






Python get file path