navigating/changing directories

skip skip at no-spam.net
Thu Jan 6 04:24:02 EST 2005


A simple script like the one below lets me jump through a directory 
structure.  However, if I run it from /this/directory and within it to go to 
/a/totally/different/directory...  I'm still actually going to be in 
/this/directory when I exit the script.  Is it possible to have a script 
that can drop me off into a different directory than where I initiated it 
from?

import os
process = 1
while (process):
    # Display directories
    for i in os.listdir(os.getcwd()):
        if (os.path.isdir(os.path.join(os.getcwd(),i))):
            print i

    # Change directory
    goto_dir = raw_input(": ")
    if (goto_dir in os.listdir(os.getcwd())):
        os.chdir(os.path.join(os.getcwd(),goto_dir))
    else:
        process = 0 # Exit





More information about the Python-list mailing list