How to get current directory in Python?

In this article, we will take a look at how to get current directory in Python. The current directory is nothing but your working directory from which your script is getting executed.

Getting the Current Working Directory in Python

The os module has a getcwd() function using which we can find out the absolute path of the working directory. 

Syntax: os.getcwd()

Parameter: None

Return Value: Returns the string which contains the absolute path of the current working directory. 

Below is an example to print the current working directory in Python

# Python program get current working directory using os.getcwd()
		
# importing os module
import os
	
# Get the current directory path
current_directory = os.getcwd()
	
# Print the current working directory
print("Current working directory:", current_directory)

Output

Current working directory: C:\Projects\Tryouts

Get the path of the script file in Python

If you want to get the path of the current script file, you could use a variable __file__ and pass it to the realpath method of the os.path module. 

Example to get the path of the current script file

# Python program get current working directory using os.getcwd()
		
# importing os module
import os
	
# Get the current directory path
current_directory = os.getcwd()
	
# Print the current working directory
print("Current working directory:", current_directory)

# Get the script path and the file name
foldername = os.path.basename(current_directory)

scriptpath = os.path.realpath(__file__)
# Print the script file absolute path
print("Script file path is : " + scriptpath)

Output

Current working directory: C:\Projects\Tryouts
Script path is : C:\Projects\Tryouts\main.py

Changing the Current Working Directory in Python 

If you want to change the current working directory in Python, use the chrdir() method.

Syntax: os.chdir(path)

Parameters: 

path: The path of the new directory in the string format.  

Returns: Doesn’t return any value

Example to change the current working directory in Python

# Import the os module
import os

# Print the current working directory
print("Current working directory: {0}".format(os.getcwd()))

# Change the current working directory
os.chdir('/Projects')

# Print the current working directory
print("New Current working directory: {0}".format(os.getcwd()))

Output

Current working directory: C:\Projects\Tryouts
New Current working directory: C:\Projects

Conclusion

To get the current working directory in Python, use the os module function os.getcwd(), and if you want to change the current directory, use the os.chrdir() method.

Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
How To Fix Keyerror In Pandas

How to Fix: KeyError in Pandas?

The KeyError in Pandas occurs when you try to access the columns in pandas DataFrame, which does not exist, or you misspell them.  Typically, we import data from the excel…
View Post
Convert Letters To Numbers In Python

Convert Letters to Numbers in Python

Table of Contents Hide Convert Letters to Numbers in PythonUsing ord() MethodUsing list comprehensionConclusion Using the ord() method, we can convert letters to numbers in Python. The ord() method returns…
View Post