Skip to content

Instantly share code, notes, and snippets.

@leiless
Last active February 22, 2024 06:18
Show Gist options
  • Save leiless/3b4d8160c4a06ce6340b03ac1ab827db to your computer and use it in GitHub Desktop.
Save leiless/3b4d8160c4a06ce6340b03ac1ab827db to your computer and use it in GitHub Desktop.
Bring bash's pushd/popd to Python3
#!/usr/bin/env python3
import os
import sys
DIR_STACK = []
def pushd(path: str):
DIR_STACK.append(os.getcwd())
os.chdir(path)
def popd():
os.chdir(DIR_STACK.pop())
if __name__ == '__main__':
pushd(os.path.dirname(os.path.realpath(sys.argv[0])))
# main()
popd()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment