Change directory in terminal using python
Asked Answered
Q

3

17

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.

Quiche answered 7/3, 2016 at 11:53 Comment(7)
Why are you doing this in Python?Louvain
It's kind of a part of a bigger project.Quiche
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 scopeWellpreserved
@RafaelCardoso exactly! Any ideas how to do it?Quiche
I guess you should use a more bash-focused script languageWellpreserved
Does using 'source python chdir.py' solves the problem?Grallatorial
@HugoSadok nope, throws an error "cannot execute binary file".Quiche
S
18

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.

Solenoid answered 7/3, 2016 at 12:15 Comment(5)
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?Quiche
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 #1506510.Solenoid
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!Quiche
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.Roley
@SamuelO'Malley Duly noted.Quiche
E
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:

Egyptology answered 11/12, 2018 at 7:0 Comment(0)
H
-1
import os
os.system('cd /home/name/projects/python')
Halimeda answered 7/3, 2016 at 11:58 Comment(3)
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')Halimeda
its a small part of a bigger project.Quiche
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.Quiche

© 2022 - 2024 — McMap. All rights reserved.