Created
January 19, 2021 05:20
-
-
Save tmorioka/3ef3c7b3fdefb7ca2d9c44a78116dc40 to your computer and use it in GitHub Desktop.
pythonでcdしたい
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import contextlib | |
import os | |
@contextlib.contextmanager | |
def cd(path): | |
current_path = os.getcwd() | |
os.chdir(path) | |
try: | |
yield | |
finally: | |
os.chdir(current_path) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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