Find path to currently running file
python relative path current directory
python 3 get current file path
python get directory from path
python set working directory command line
nameerror: name '__file__' is not defined
get current path
How can I find the full path to the currently running Python script? That is to say, what do I have to do to achieve this:
$ pwd /tmp $ python baz.py running from /tmp file is baz.py
__file__ is NOT what you are looking for. Don't use accidental side-effects
sys.argv is always the path to the script (if in fact a script has been invoked) -- see http://docs.python.org/library/sys.html#sys.argv
__file__ is the path of the currently executing file (script or module). This is accidentally the same as the script if it is accessed from the script! If you want to put useful things like locating resource files relative to the script location into a library, then you must use
C:\junk\so>type \junk\so\scriptpath\script1.py import sys, os print "script: sys.argv is", repr(sys.argv) print "script: __file__ is", repr(__file__) print "script: cwd is", repr(os.getcwd()) import whereutils whereutils.show_where() C:\junk\so>type \python26\lib\site-packages\whereutils.py import sys, os def show_where(): print "show_where: sys.argv is", repr(sys.argv) print "show_where: __file__ is", repr(__file__) print "show_where: cwd is", repr(os.getcwd()) C:\junk\so>\python26\python scriptpath\script1.py script: sys.argv is 'scriptpath\\script1.py' script: __file__ is 'scriptpath\\script1.py' script: cwd is 'C:\\junk\\so' show_where: sys.argv is 'scriptpath\\script1.py' show_where: __file__ is 'C:\\python26\\lib\\site-packages\\whereutils.pyc' show_where: cwd is 'C:\\junk\\so'
Python Directory & File Management, On Windows, you can check the current directory with the dir command instead of pwd . Create a Python script file ( file_path.py ) with the following contents in the lower directory ( data/src ). Run it with the python command. The absolute path of the current working directory can be obtained with os. 31. The correct solution is to use the get_included_files function: list ($scriptPath) = get_included_files (); This will give you the absolute path of the initial script even if: This function is placed inside an included file. The current working directory is different from initial script's directory.
This will print the directory in which the script lives (as opposed to the working directory):
import os dirname, filename = os.path.split(os.path.abspath(__file__)) print "running from", dirname print "file is", filename
Here's how it behaves, when I put it in
> cd c:\src > python so-where.py running from C:\src file is so-where.py > cd c:\ > python src\so-where.py running from C:\src file is so-where.py
Two double underscore variables, How do I get the current file path in Python? What powershell code can I insert in the script, that it will print what ever current location/path of the script. (The goal is to make this script portable) ie: -When in folderA, the code will print C:\FolderA (full-path)-When Script1.ps1 is moved to C:\FolderB\FolderC and run, it prints the full path of C:\FolderB\FolderC
import sys, os file = sys.argv pathname = os.path.dirname(file) print 'running from %s' % os.path.abspath(pathname) print 'file is %s' % file
Check os.getcwd() (docs)
it as a bytes object, we use the method getcwdb(). Inside, there is the batch files and another folder with the files needed to run the batch. Normally, when you make a batch, you type the path where the files are. But I won't know where the files are. The files will still be kept inside the master folder, but I need to have the batch find that folder to run the files.
The running file is always
Here's a demo script, named
Here's the results
MacBook-5:Projects slott$ python StackOverflow/identify.py StackOverflow/identify.py MacBook-5:Projects slott$ cd StackOverflow/ MacBook-5:StackOverflow slott$ python identify.py identify.py
that Python is currently importing. You can use this variable inside a module to find the path of the module. For example, let's say you have a module like this: Contents of example_module.py. Can a Bash script tell what directory it's stored in? In a Windows command script, one can determine the directory path of the currently executing script using %~dp0. For example: @echo Running from %~dp0 What would be the equivalent in a BASH script?
I would suggest
import os, sys print os.path.split(os.path.abspath(os.path.realpath(sys.argv)))
This way you can safely create symbolic links to the script executable and it will still find the correct directory.
Python 101: How to Find the Path of a Running Script. October 29, 2013 script_path = os.path.dirname(os.path.abspath( __file__ )). import os script_path This works for me and is what I currently use. The code above You should not use Directory.GetCurrentDirectory() in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut. It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); for your purpose. This returns the pathname where the currently executing assembly resides.
In today's post we will see how to find current directory(or working directory) Copy the above code into a file and save it as get_path.py and run it to to get the path of script it self irrespective of from where you are running it. For example, if your current directory is C:\Windows and you run the script \\server\share\somepath\myscript.cmd then any relative paths in the script file won’t work correctly. One way around this is to make the script change working directory right at the start and then use relative paths after that.
On this page: open(), file path, CWD ('current working directory'), r 'raw string' prefix When you execute your script, your CWD is set to the directory where your script is. Below illustrates how to find your CWD (.getcwd()) and change it into a The absolute path of the current working directory can be obtained with os.getcwd(), and the relative path from the current working directory of the running script file can be obtained with __file__. In the following examples, add code to the same script file (file_path.py), and execute from the same directory as the above example.
Now I want to get path to the m-file from inside the script. As a note, this solution will work for a script that you are currently executing, but would not be Path normalization. Skipping normalization. Case and the Windows file system. Members of many of the types in the System.IO namespace include a path parameter that lets you specify an absolute or relative path to a file system resource. This path is then passed to Windows file system APIs. This topic discusses the formats for file paths that you can use on Windows systems.
- possible duplicate of How to get full path of current file's directory in Python? This came earlier, but that is more explicit. Or: stackoverflow.com/questions/50499/…
- Agreed! Sorry it took my a while to come around to it; I had taken a bit of a break from the code that used I need this for. But after much deliberation, os.getcwd() did exactly what I needed and not file, which instead gave me the name of the file that function was in (not the file being run).
- As a note, if you're looking for the path to the file that holds the specific line of code being run,
__file__is the one to use. (Such as, an
__init__.pywhere you don't know where it's going to live and need to add it's
sys.pathwhen that module is imported...)
- @MarkRibau: Your first sentence is true but superflous (that's what
__file__is defined to be). Your 2nd sentence is nothing to with the question, and is extremely puzzling: if you don't know where a module is, you can't import it, and if you can't import it you can't get its
- @JohnMachin "currently running python script" is a vague question. Currently running line? Currently running
__main__? Which is why I clarify that if you intend currently running line,
- @JohnMachin Also, you can import any module that is on
sys.pathwithout knowing it's path. HOWEVER, some modules (such as
p4python) require it's own path to be in
sys.path. I felt it silly to go around and modify everyone's systems to have that in their environment, so instead I made an
__init.py__that will do it automatically no matter where their project root is. We do not require our developers to have identical locations for the project root, as different contractors have different hardware / harddrive capacities.
- Note that this fails when running in py2exe, if you ever plan on using that. In that case you can import a dummy module alongside the main module and get its file attribute and follow the same process, though the module will be contained in a .zip file, so you'll have to split it a bit differently.
- @FogleBird: What's wrong with avoiding
__file__altogether and following this: py2exe.org/index.cgi/WhereAmI ??
- Well, my way works both in py2exe and outside of it without any conditional checks. Also, py2exe docs are very scattered and hard to find what you're looking for.
__file__is a KLUDGE. Importing a dummy module is a KLUDGE. "you'll have to split it a bit differently" == "without any conditional checks"?? Hard to find?? here's another link:
- But you could be executing the script from another directory, and argv would return the script's path, not yours.
- @willehr: The script's path is EXACTLY what the OP requires.
- @Daz: (1) syntax error (2) if the absolute path to the script is e.g
r"C:\myfiles\fubar.py", the basename is
"fubar.py"-- is that what you call "the path of your running script"?