How to set the current working directory?
You can set the current working directory in Python using the os
module, specifically the chdir()
function. Here is an example code snippet:
import os
# Set the current working directory to "/path/to/directory"
os.chdir("/path/to/directory")
Watch a video course
Python - The Practical Guide
You can also use os.getcwd()
to get the current working directory.
import os
# Get the current working directory
current_directory = os.getcwd()
print(current_directory)
You can also use pathlib module to set the current working directory.
from pathlib import Path
# Set the current working directory to "/path/to/directory"
Path.cwd()
Please note that the chdir()
function only changes the current working directory for the current process, not for the entire system.