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 '/'
3 Comments
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…
Thanks fan! I’ve never heard of iPython, but I’m checking it out as I type this.
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!