Skip to content

Instantly share code, notes, and snippets.

@tmorioka
Created January 19, 2021 05:20
Show Gist options
  • Save tmorioka/3ef3c7b3fdefb7ca2d9c44a78116dc40 to your computer and use it in GitHub Desktop.
Save tmorioka/3ef3c7b3fdefb7ca2d9c44a78116dc40 to your computer and use it in GitHub Desktop.
pythonでcdしたい
import contextlib
import os
@contextlib.contextmanager
def cd(path):
current_path = os.getcwd()
os.chdir(path)
try:
yield
finally:
os.chdir(current_path)
import os
from cd import cd
def main():
print(os.getcwd())
with cd("/path/you/wanna/go"):
print(os.getcwd())
print(os.getcwd())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment