How to get current directory in Python?
Tags: python, programming, codenewbie, tutorial
Published: 2021-09-21 06:50:55

---
title: How to get current directory in Python?
published: true
date: 2021-09-19 16:20:46 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/how-to-get-current-directory-in-python/
---

ItsMyCode |

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 
# 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 
# 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**

```python 
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

```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**

```python 
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.

The post [How to get current directory in Python?](https://itsmycode.com/how-to-get-current-directory-in-python/) appeared first on [ItsMyCode](https://itsmycode.com).