Get directory of current Python script
In this article, we will cover How to Get and Change the Current Working Directory in Python. While working with file handling you might have noticed that files are referenced only by their names, e.g. ‘GFG.txt’ and if the file is not located in the directory of the script, Python raises an error.
The concept of the Current Working Directory (CWD) becomes important here. Consider the CWD as the folder, the Python is operating inside. Whenever the files are called only by their name, Python assumes that it starts in the CWD which means that a name-only reference will be successful only if the file is in the Python’s CWD.
Note: The folder where the Python script is running is known as the Current Directory. This may not be the path where the Python script is located.
What is the Python os module?
Python provides os module for interacting with the operating system. This module comes under Python’s standard utility module. All functions in the os module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted by the operating system.
Using os.getcwd() method to get Python Script location
The os.getcwd() method is used for getting the Current Working Directory in Python. The absolute path to the current working directory is returned in a string by this function of the Python OS module.
Syntax of os.getcwd() method
Syntax: os.getcwd()
Parameter: No parameter is required.
Return Value: This method returns a string which represents the current working directory.
Example 1: Get the current working directory using os.getcwd()
I have placed the Python file (test.py) inside /home/tuhingfg/Documents/Scripts/test.py. And running the Python Script from the Scripts folder.
Python3
import os print ( "File location using os.getcwd():" , os.getcwd()) |
Output:
File location using os.getcwd(): /home/tuhingfg/Documents/Scripts
Note: Using os.getcwd() doesn’t work as expected when running the Python code from different directory as the Python script.
Example 2: Unexpected result when running Python script from different directory other than script using os.getcwd()
Python3
import os print ( "File location using os.getcwd():" , os.getcwd()) |
Output:

Get Script location using os.getcwd()
Explanation: The Python script is placed inside /home/tuhingfg/Documents/Scripts. When we run the script from inside the same folder, it gives correct script location. But when we change our directory to some other place, it outputs the location of that directory. This is because, os.getcwd() considers the directory from where we are executing the script. Based on this, the result of os.getcwd() also varies.
Using os.path.realpath() method to get Python Script location
os.path.realpath() can be used to get the path of the current Python script. Actually, os.path.realpath() method in Python is used to get the canonical path of the specified filename by eliminating any symbolic links encountered in the path. A special variable __file__ is passed to the realpath() method to get the path of the Python script.
Note: __file__ is the pathname of the file from which the module was loaded if it was loaded from a file.
Syntax of os.path.realpath() method
Syntax: os.path.realpath(path)
Parameter:
- path: A path-like object representing the file system path. A path-like object is either a string or bytes object representing a path.
Return Type: This method returns a string value which represents the canonical path.
Example: Get the directory script path using __file__ special variable
Inside a Python script, we can use os.path.realpath() and __file__ special variable to get the location of the script. And the result obtained from this method doesn’t depend on the location of execution of the script.
Python3
import os print ( "File location using os.getcwd():" , os.getcwd()) print (f "File location using __file__ variable: {os.path.realpath(os.path.dirname(__file__))}" ) |
Output:

Explanation: Here, the os.getcwd() and __file__ provides two different results. Since, we are executing the script from a different folder than the script, os.getcwd() output has changed according to the folder of execution of the script. But __file__ generates the constant result irrespective of the current working directory.
Please Login to comment...