How do I change my current directory from a python script?
Posted
by misterloogs
on Stack Overflow
See other posts from Stack Overflow
or by misterloogs
Published on 2010-04-03T14:38:37Z
Indexed on
2010/04/03
14:43 UTC
Read the original article
Hit count: 219
I'm trying to implement my own version of the 'cd' command that presents the user with a list of hard-coded directories to choose from, and the user has to enter a number corresponding to an entry in the list. The program, named my_cd.py
for now, should then effectively 'cd' the user to the chosen directory. Example of how this should work:
/some/directory
$ my_cd.py
1) ~
2) /bin/
3) /usr
Enter menu selection, or q to quit: 2
/bin
$
Currently, I'm trying to 'cd' using os.chdir('dir')
. However, this doesn't work, probably because my_cd.py
is kicked off in its own child process. I tried wrapping the call to my_cd.py
in a sourced bash script named my_cd.sh
:
#! /bin/bash
function my_cd() {
/path/to/my_cd.py
}
/some/directory
$ . my_cd.sh
$ my_cd
... shows list of dirs, but doesn't 'cd' in the interactive shell
Any ideas on how I can get this to work? Is it possible to change my interactive shell's current directory from a python script?
© Stack Overflow or respective owner