The working directory (CWD) can be replaced with os.chdir(path)
When I change CWD, It is often used as a way to change the old path (CWD) to a different location (newpath) and then return to the old path.
import os
class chdir(object):
def __init__(self, newpath):
self.oldPath = os.getcwd()
os.chdir(newpath)
def saveImage(self, img):
pass #
def __del__(self):
os.chdir(self.oldPath) #(Caution) Error if oldPath disappeared during operation.
myClass = chdir("/")
#myClass.saveImage()
*Changing the CWD in the subprocess has no effect on the current process.
That is, when you run a Python script that performs os.chdir()
in the shell,
The CWD in the Python program only changes, but the CWD in the shell does not change.
© 2024 OneMinuteCode. All rights reserved.