How to Set the Current Working Directory in Python

Created
Modified

Using chdir Method

The os.chdir(path) method changes the current working directory to path. For example,

#!/usr/bin/python3

# Import module
import os

os.chdir("/data/site")

  

Prints the current working directory:

#!/usr/bin/python3

# Import module
import os

print(os.getcwd())
/home/data/python

Related Tags