18

I'm writing a simple script to change the present working directory to some other directory. The following script works okay until the program terminates, after which I'm back to my home directory.

#!/usr/bin/python

import os

if __name__ == '__main__':
    os.chdir("/home/name/projects/python")
    os.system("pwd")
    print 'dir changed'

Output is:

bash:~$ python chdir.py
/home/name/projects/python
dir changed
bash:~$ pwd
/home/name

I want the directory change to remain even after the program has exited. Any ideas how to do it?

Edit: What I really want to do is this: I use this directory frequently and instead of doing cd <path> every time I open the terminal, I just write ./progname and it changes the directory.

7
  • 2
    Why are you doing this in Python? Commented Mar 7, 2016 at 11:55
  • It's kind of a part of a bigger project. Commented Mar 7, 2016 at 12:04
  • Oh, I see what you're trying to do. Python is actually changing the dir inside its scripts scope, but not on your terminal's scope Commented Mar 7, 2016 at 12:06
  • @RafaelCardoso exactly! Any ideas how to do it? Commented Mar 7, 2016 at 12:12
  • I guess you should use a more bash-focused script language Commented Mar 7, 2016 at 12:13

4 Answers 4

19

If you want the directory change to remain even after the program has exited. You can end the python script with os.system("/bin/bash"), this will leave you in bash shell inside the new directory.

#!/usr/bin/python
import os
if __name__ == '__main__':
    os.chdir("/home/name/projects/python")
    os.system("pwd")
    os.system("/bin/bash")

For the need raised in your comment "I use this directory frequently and instead of doind cd <path> every time I open the terminal, I just write ./progname and it changes the directory"
I would suggest using bash alias which will change directory:

bash:~$ alias mycd='cd /home/name/projects/python'

and use this alias in bash shell in order to change the directory:

bash:~$ mycd

You can add this alias to your .bashrc - which will allow you to use this alias every time.

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

5 Comments

This works! Thank you very much. Can you explain me what the last command is doing? Is it creating a new instance of terminal or what?
It opens a new bash shell as a sub-process, hence it uses the new directory. It answers you questions, but I'm not sure how useful will it be for a large scale project. For more info see stackoverflow.com/questions/1506010/….
I knew it could be done by using bash alias, i just wanted to know how to do it with python. Both of your solutions work. Thanks again!
Please note @KrishanuKonar that this is not actually changing your directory of your current bash session. It is creating a new bash process in the directory you want. What you are actually asking for is impossible with Python, but extremely easy with bash.
@SamuelO'Malley Duly noted.
0

In case someone would like to do this without python - this is most simply done with a .bash_profile file.

Steps:

  1. Type this into your .bash_profile. You can open this file with pico.
pico ~/.bash_profile
  1. Then create a shortcut (called an alias), you can do whatever phrase you want.
alias cdd="cd ~/frequent/my-directory"
  1. Then source your .bash_profile file.
source ~/.bash_profile

Now, you just run your aforementioned shortcut, and this switches your directory, with many less key strokes!

Macbook-Spen:~ spen$ cdd
Macbook-Spen:my-directory spen$ 

Sources:

Comments

0

Make the Python script output the new directory

#!/usr/bin/python

import os

if __name__ == "__main__":
    os.chdir("/home/user/python/scripts/script.py")
    new_path: str = os.getcwd()
    print(new_path)

Then write a bash wrapper. This way, the cd operation isn't hidden in a subshell. For cd to operate properly, use it in a function:

#!/usr/bin/env bash
mycd() {
    script_output="$(/home/user/python/scripts/script.py)"
    # test if output is dir
    if [ -d "$script_output" ]; then
        cd "$script_output"
    else
        echo "failed to change directory to $script_output"
    fi
}

then in terminal:

  1. chmod 775 bash_wrapper

  2. source bash_wrapper

Now you can use mycd (not the script names) to invoke your operation.

Comments

-1
import os
os.system('cd /home/name/projects/python')

3 Comments

why you want to do it? if you want to run another command on projects/python directory it will after this os.system('cd /home/name/projects/python') You can use os.system('Another command')
its a small part of a bigger project.
I know it'll work after that, I need to preserve the directory change after the program has exited. Think of it like this: I use this directory frequently and instead of doind cd <path> everytime i open the terminal, i just write ./progname and it changes the directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.