current working directory

Changing and Getting Working Directory in Python

Changing and Getting Working Directory in Python

To work with directories in Python, you need to use the os module. Here’s how you can get and change the current working directory:

Getting the Current Working Directory

import os

# Get the current working directory
current_directory = os.getcwd()
print('Current Directory:', current_directory)

Changing the Current Working Directory

# Change the working directory
new_directory = "/path/to/your/desired/directory"
os.chdir(new_directory)

# Verify the change
current_directory = os.getcwd()
print('Directory after change:', current_directory)

Remember to replace "/path/to/your/desired/directory" with the actual path where you want to change the directory.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top