Created
May 9, 2019 17:53
-
-
Save siennathesane/c895b90d1df65f1e741267d395b3e674 to your computer and use it in GitHub Desktop.
Directory stacks in Python
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
| # directory stack | |
| _directory_stack = [] | |
| def pushd(target: str): | |
| """ | |
| Push a new directory to the top of the stack and changes to it. | |
| Arguments: | |
| target {str} -- Where you want to go | |
| """ | |
| _directory_stack.append(path.join(path.abspath("."), target)) | |
| chdir(target) | |
| def popd(): | |
| """ | |
| Pops the most recent directory off the stop of the stack and returns you to the previous directory | |
| """ | |
| if len(_directory_stack) > 1: | |
| _directory_stack.pop(-1) | |
| chdir(_directory_stack[-1]) | |
| if len(_directory_stack) == 1: | |
| chdir(_directory_stack[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment