Get directory of current Python script

01 Jul, 2022
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. So, How is it done? 
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 is not the path where the Python script is located.
Python provides an 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.
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: os.getcwd()
Parameter: No parameter is required.
Return Value: This method returns a string which represents the current working directory. 
Example: Get the current working directory using os.getcwd()
Python program to explain os.getcwd() method.
Python3
# importing os moduleimport os # Get the current working# directory (CWD)cwd = os.getcwd() # Print the current working# directory (CWD)print("Current working directory:")print(cwd)
Output:
 
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: 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 os.getcwd()
Python program to get the path of the script
Python3
import os # Get the current working# directory (CWD)cwd = os.getcwd()print("Current Directory:", cwd) # Get the directory of# scriptscript = os.path.realpath(__file__)print("SCript path:", script)
Output:
 
The current working directory in Python is changed to the specified path using os.chdir() method. As a new directory path, it only accepts one argument.
Syntax: os.chdir(path)
Parameters: 
path: A complete path of directory to be changed to new directory path.
Returns: Doesn’t return any value
Example: Change the current working directory os.chdir()
In this example, we try to change our working directory to gfg_dir.
Python3
# import os moduleimport os # change the current working directory# to specified pathos.chdir('c:\\gfg_dir') # verify the path using getcwd()cwd = os.getcwd() # print the current directoryprint("Current working directory is:", cwd)
Output:
Current working directory is: c:\\gfg_dir
nidhi_biet
singghakshay
surajkumarguptaintern
Python file-handling-programs
Python os-module-programs
python-file-handling
python-os-module
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
How to Install PIP on Windows ?
Python String | replace()
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Iterate over a list in Python