Skip to content

Find the current directory and file's directory

To find the current directory in Python, you can use the following code:

Find the current directory in Python using the os.getcwd method

python
import os
current_directory = os.getcwd()
print(current_directory)

<div class="alert alert-info flex not-prose"> Watch a course Python - The Practical Guide</div>

To find the directory of the current file in Python, you can use the following code:

Find the directory of the current file in Python using os.path object

python
import os
current_file_directory = os.path.dirname(os.path.abspath(__file__))
print(current_file_directory)

Dual-run preview — compare with live Symfony routes.