0

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

Dipak Telangre
2,0134 gold badges25 silver badges49 bronze badges
asked Aug 9, 2021 at 3:28
1

2 Answers 2

1

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

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

Comment if this helps.

answered Aug 9, 2021 at 3:40
Sign up to request clarification or add additional context in comments.

2 Comments

When I run your code, I received this error: AttributeError: module 'os' has no attribute 'cwd'
it should be os.getcwd(). Thanks
1

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
atline
32.1k19 gold badges102 silver badges134 bronze badges
answered Aug 9, 2021 at 3:39

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.