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

4
  • 1
    Why do you need the os.system(unixshell) command? os.chdir(dir) should be fine on it's own. Commented Apr 25, 2017 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. Commented Apr 25, 2017 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. Commented Apr 25, 2017 at 13:16
  • 1
    Oh I see, you likely will need to take the advice of n.m. and do something similar to this: stackoverflow.com/a/3786928/499581, or stackoverflow.com/a/2571529/499581. Commented Apr 25, 2017 at 13:39

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'll probably do that, but only with sadness, since my python script seemed so nice.
@Magicsowon: in general it better to do bash things in bash and python things in python
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.