In Python the working directory can be changed by following way:-
import os
os.chdir(path)
When the current working directory in a subprocess changed it does not change the current working directory in the parent process.
Another way it can be done by using contextmanager:-
contextmanager:-
Context managers in Python facilitate the proper handling of resources like file operations or database connections.
import os
from contextlib import contextmanager
@contextmanager
def working_directory(directory):
owd = os.getcwd()
try:
os.chdir(directory)
yield directory
finally:
os.chdir(owd)
To know more about this you can have a look at the following video tutorial:-