Change current working directory from inside the Python interpreter

This is comes in very handy when working in the Python interpreter:


>>> import os
>>> os.getcwd() # Returns the current working directory; usually the directory you were in when you started the interpreter
>>> os.chdir('/path/to/directory') # Change the current working directory to 'path/to/directory'. Also accepts bash commands like '..' and '/'

This entry was posted in Python and tagged . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

3 Comments

  1. Evan Jones
    Posted October 5 2009 at 5:45 pm | Permalink

    Dude, are you using iPython? If you’re spending time in the interpreter and NOT using iPython, your life is sadder than it ought to be…

  2. Posted October 5 2009 at 5:58 pm | Permalink

    Thanks fan! I’ve never heard of iPython, but I’m checking it out as I type this.

  3. Evan Jones
    Posted October 6 2009 at 9:31 pm | Permalink

    iPython makes my life much happier. It’s worth spending some time with:

    – method tab-completion
    – run shell commands (‘ls’, ‘cd xxx’, etc) from the interpreter
    – drop into your editor whenever you want to refine code interactively
    – easily save and reuse a session, so if you did a bunch of fancy stuff in the interpreter you can automatically put that in a file.
    – automatically drop into a debugger when something goes wrong.
    – Yay!

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>