16

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.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Krishh
  • 602
  • 1
  • 8
  • 25

3 Answers3

17

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.

Yaron
  • 10,166
  • 9
  • 45
  • 65
  • 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? – Krishh Mar 07 '16 at 12:19
  • 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 http://stackoverflow.com/questions/1506010/how-to-use-export-with-python-on-linux. – Yaron Mar 07 '16 at 12:23
  • 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! – Krishh Mar 07 '16 at 12:47
  • 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. – Samuel O'Malley Mar 07 '16 at 12:56
  • @SamuelO'Malley Duly noted. – Krishh Mar 07 '16 at 13:11
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:

spen.smith
  • 576
  • 2
  • 16
-1
import os
os.system('cd /home/name/projects/python')
Himanshu dua
  • 2,496
  • 1
  • 20
  • 27
  • 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') – Himanshu dua Mar 07 '16 at 12:05
  • its a small part of a bigger project. – Krishh Mar 07 '16 at 12:07
  • 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 everytime i open the terminal, i just write ./progname and it changes the directory. – Krishh Mar 07 '16 at 12:11