Python os Module
os.path
Find current path:
# Import the os module
import os
# Get the current working directory
cwd = os.getcwd()
Change path:
# Import the os module
import os
# Change the current working directory
os.chdir('/tmp')
The argument provided to the chdir() method must be a directory; otherwise NotADirectoryError exception is raised. If the specified directory doesn’t exist, a FileNotFoundError exception is raised. If the user under which the script is running doesn’t have the necessary permissions, a PermissionError exception is raised.
# Import the os module
import os
path = '/var/www'
try:
os.chdir(path)
print("Current working directory: {0}".format(os.getcwd()))
except FileNotFoundError:
print("Directory: {0} does not exist".format(path))
except NotADirectoryError:
print("{0} is not a directory".format(path))
except PermissionError:
print("You do not have permissions to change to {0}".format(path))