0

I am trying to create Python script jump.py that allows me to jump to a set of predefined dirs, and looks like it is not possible to do this, because after scripts exits, the previous directory is restored:

import os

print(os.getcwd())
os.chdir('..')
print(os.getcwd())

[micro]$ python jump.py 
/home/iset/go/src/github.com/zyedidia/micro
/home/iset/go/src/github.com/zyedidia
[micro]$ 

Is that at all possible to land to [zyedidia] dir after the script finishes?

anatoly techtonik
  • 19,847
  • 9
  • 124
  • 140
  • See [this horrible hack](http://stackoverflow.com/questions/431684/how-do-i-cd-in-python)...., the answer is at the bottom with 5 votes (as of today) – 576i Sep 21 '16 at 08:24
  • Is it mandatory to use Python for your task ? – Simon PA Sep 21 '16 at 08:54
  • @SimonPA I'd like to keep that cross-platform if possible and not requiring compilation. – anatoly techtonik Sep 21 '16 at 08:56
  • Alright, it's tricky to achieve that because the Python interpreter will always restore the property of the shell at the start of the process. I see in your exemple that you use your script in a Git repository. Is it meant to be use in Git hooks ? If yes, We might be able to find a workaround there – Simon PA Sep 21 '16 at 09:00
  • @576i horrible hack is not a horrible answer. =) – anatoly techtonik Sep 21 '16 at 09:01
  • @SimonPA it is meant to quickly jump between different upstream projects from command line. Previously I would just stack every projects under `~/p`, but [golang] ruins the workflow, which is inconvenient anyway. – anatoly techtonik Sep 21 '16 at 09:03

1 Answers1

0

You can try this;

import os

print(os.getcwd())
os.chdir('..')
print(os.getcwd())
shell = os.environ.get('SHELL', '/bin/sh')
os.execl(shell, shell)
Mustafa DOGRU
  • 3,994
  • 1
  • 16
  • 24