Equivalent of shell 'cd' command to change the working directory?
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping
--
Chapters
00:00 Question
00:22 Accepted answer (Score 911)
01:21 Answer 2 (Score 358)
02:14 Answer 3 (Score 153)
02:45 Answer 4 (Score 153)
03:06 Thank you
--
Full question
https://stackoverflow.com/questions/4316...
Accepted answer links:
[his answer]: https://stackoverflow.com/questions/4316...
Answer 2 links:
[ActiveState version]: http://code.activestate.com/recipes/5766...
[more concise equivalent(below)]: https://stackoverflow.com/a/24176022/263...
[ContextManager]: https://docs.python.org/2/library/contex...
Answer 4 links:
[here]: http://effbot.org/librarybook/os.htm
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #cd
#avk47
ACCEPTED ANSWER
Score 944
You can change the working directory with:
import os
os.chdir(path)
You should be careful that changing the directory may result in destructive changes your code applies in the new location. Potentially worse still, do not catch exceptions such as WindowsError and OSError after changing directory as that may mean destructive changes are applied in the old location!
If you're on Python 3.11 or newer, then consider using this context manager to ensure you return to the original working directory when you're done:
from contextlib import chdir
with chdir(path):
# do stuff here
If you're on an older version of Python, Brian M. Hunt's answer shows how to roll your own context manager: his answer.
Changing the current working directory in a subprocess does not change the current working directory in the parent process. This is true of the Python interpreter as well. You cannot use os.chdir() to change the CWD of the calling process.
ANSWER 2
Score 362
Here's an example of a context manager to change the working directory. It is simpler than an ActiveState version referred to elsewhere, but this gets the job done.
Context Manager: cd
import os
class cd:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
Or try the more concise equivalent(below), using ContextManager.
Example
import subprocess # just to call an arbitrary command e.g. 'ls'
# enter the directory like this:
with cd("~/Library"):
# we are in ~/Library
subprocess.call("ls")
# outside the context manager we are back wherever we started.
ANSWER 3
Score 155
I would use os.chdir like this:
os.chdir("/path/to/change/to")
By the way, if you need to figure out your current path, use os.getcwd().
More here
ANSWER 4
Score 27
If you're using a relatively new version of Python, you can also use a context manager, such as this one:
from __future__ import with_statement
from grizzled.os import working_directory
with working_directory(path_to_directory):
# code in here occurs within the directory
# code here is in the original directory
UPDATE
If you prefer to roll your own:
import os
from contextlib import contextmanager
@contextmanager
def working_directory(directory):
owd = os.getcwd()
try:
os.chdir(directory)
yield directory
finally:
os.chdir(owd)