0
votes

I am using this code to get into the directory in Python:

''' os.chdir(path) '''

Then, I want to exit this directory to the last directory. What do I have to do? Thanks

2

2 Answers

0
votes

Yes. You can run the code os.cwd() before the given line and store into a variable. and cd into this after.

import os
# ...
originalPath = os.cwd()
os.chdir(path)
# process your task
os.chdir(originalPath)

Comment if this helps.

0
votes

I understood your problem , this may help.

import os

curr = os.getcwd() # this returns current working directory in which this code              #is.store it in curr variable

os.chdir('../') # this will change working directory to specified path.

os.chdir(curr) #now if you wnat to go back to your directory use this