[Tutor] Setting Path From Python Shell (IDLE 0.8)

Bruce Sass bsass@freenet.edmonton.ab.ca
Sat, 19 May 2001 12:16:11 -0600 (MDT)


On Sat, 19 May 2001, Mr. Razak wrote:

> I want to know, how can i change directory while working inside python shell.
> Let say i want to save my python program in C:\PY_PRG directory and
i want to set default to this directory. How to do that.

The following sequence should help...

Python 2.1 (#4, Apr 25 2001, 03:51:07)
[GCC 2.95.4 20010319 (Debian prerelease)] on linux2
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> import os
>>> import sys
>>> sys.executable
'/usr/local/bin/python2.1'
>>> os.path.dirname(sys.executable)
'/usr/local/bin'
>>> os.getcwd()
'/home/bsass'
>>> os.chdir(os.path.dirname(sys.executable))
>>> os.getcwd()
'/home/usr/local/bin'

NOTE: /usr/local is a symlink to /home/usr/local on this box
(just one of the things to keep in mind when playing with paths).

So, you can use ``os.chdir("C:\some\path")'' manually from the "Python
Shell" prompt; or you can have PY_PRG set its own current working
directory based on sys.argv[0] (I used sys.executable because sys.argv
only makes sense while a program is running).


- Bruce