Skip to content

Instantly share code, notes, and snippets.

@siennathesane
Created May 9, 2019 17:53
Show Gist options
  • Save siennathesane/c895b90d1df65f1e741267d395b3e674 to your computer and use it in GitHub Desktop.
Save siennathesane/c895b90d1df65f1e741267d395b3e674 to your computer and use it in GitHub Desktop.
Directory stacks in Python
# 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