2

I have the following piece of code:

import os

unixshell=os.environ["SHELL"]
dir="/home/user/somewhere"
if os.path.isdir(dir):
    os.chdir(dir)
    os.system(unixshell)

This is part of a script I wrote to bookmark folders I visit often in the terminal. A part of the script goes (cd's) to the bookmarked directory. I use it in the following way

~/olddir$ bk.py go [handle to bookmarked directory]
~/bookmarkeddir$

But, the thing I don't like is that a new bash shell is created, so when I need logout, I have to press CTRL+D multiple times.

The question is, can I change directory without creating a new shell? Is this possible using modules existent in python 2.4, or do I need to go to a higher version?

Edit: My question is more duplicate of this question:

Change directory of a parent process from a child process

Community
  • 1
  • 1
user3653831
  • 235
  • 6
  • 19
  • 1
    Why do you need the `os.system(unixshell)` command? `os.chdir(dir)` should be fine on it's own. – l'L'l Apr 25 '17 at 13:05
  • 2
    You need to create a *bash function* and change directory inside the function. The function may use external tools such as python scripts in order to determine which directory to change to. – n. m. could be an AI Apr 25 '17 at 13:06
  • @l'L'l I run the command from the terminal and after I run the python script, the directory should be the one I bookmarked. The goal is to improve my navigation in the terminal. The problem is that this script opens a new bash shell. I want the same functionality, but without opening a new bash shell from the old shell via this script. – user3653831 Apr 25 '17 at 13:16
  • 1
    Oh I see, you likely will need to take the advice of n.m. and do something similar to this: http://stackoverflow.com/a/3786928/499581, or http://stackoverflow.com/a/2571529/499581. – l'L'l Apr 25 '17 at 13:39

1 Answers1

1

The problem is that python runs in a child process, and it "can't" alter the current directory of the parent (I say "can't", but there are probably some debuggers that could do it - don't go there!).

The simplest solution is to use a function instead. For example:

bk() {
    dir="/home/user/somewhere"

    # equivalent to "if os.path.isdir(dir): os.chdir(dir)"
    [[ -d $dir ]] && cd "$dir"
}

Put this into a file called bk.sh (for example).

To compile and load the function do:

. bk.sh

Then use bk whenever you want to change directory.

The difference with a function is that it runs in the current process, it does not create a new shell.

cdarke
  • 42,728
  • 8
  • 80
  • 84
  • I'll probably do that, but only with sadness, since my python script seemed so nice. – user3653831 Apr 25 '17 at 17:56
  • @Magicsowon: in general it better to do bash things in bash and python things in python – cdarke Apr 25 '17 at 18:03
  • I ended up using this solution with my python script. My script write this function to .bashrc the first time it's invoked. I also added this in the function: `dir=\`bk.py show $1\``. That way the function takes the handle for the directory in my list and cd's to that directory. – user3653831 Apr 29 '17 at 13:34