[Python] Change directory in terminal using python

Discussão em 'Python' iniciado por Stack, Maio 16, 2025.

  1. Stack

    Stack Membro Participativo

    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.

    Continue reading...

Compartilhe esta Página