Skip to content

Instantly share code, notes, and snippets.

@Tatsh
Created October 24, 2013 05:26
Show Gist options
  • Select an option

  • Save Tatsh/7131812 to your computer and use it in GitHub Desktop.

Select an option

Save Tatsh/7131812 to your computer and use it in GitHub Desktop.

Revisions

  1. Tatsh created this gist Oct 24, 2013.
    25 changes: 25 additions & 0 deletions pushd.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    from os import chdir, getcwd
    from os.path import realpath

    class PushdContext:
    cwd = None
    original_dir = None

    def __init__(self, dirname):
    self.cwd = realpath(dirname)

    def __enter__(self):
    self.original_dir = getcwd()
    chdir(self.cwd)
    return self

    def __exit__(self, type, value, tb):
    chdir(self.original_dir)

    def pushd(dirname):
    return PushdContext(dirname)

    # Example use
    with pushd('./anfplaylists') as ctx:
    print(ctx.cwd, ctx.original_dir)
    print(getcwd())