By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,089 Members | 2,359 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,089 IT Pros & Developers. It's quick & easy.

navigating/changing directories

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
> /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?


Use os.chdir(newpath)

So, you can code os.chdir(r'/a/totally/different/directory') and find
yourself in /a/totally/different/directory after the script completes.
Thanks,
--Kartic

Jul 18 '05 #2

P: n/a
Hmmm... I take it back... that is not working! I think it changes the
path only during execution time.

Jul 18 '05 #3

P: n/a
The script is executed in a process separate from your command shell, and hence
has no effect on your shell's current directory.

There are some things that batch files and shell scripts are still good for -
manipulating the shell :)

Cheers,
Nick.

--
Nick Coghlan | nc******@email.com | Brisbane, Australia
---------------------------------------------------------------
http://boredomandlaziness.skystorm.net
Jul 18 '05 #4

P: n/a
In article <ma**************************************@python.o rg>,
Nick Coghlan <nc******@email.com> wrote:
The script is executed in a process separate from your command shell, and hence
has no effect on your shell's current directory.

There are some things that batch files and shell scripts are still good for -
manipulating the shell :)

Jul 18 '05 #5

P: n/a
skip wrote:
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

As has already been reported, under Linux a change to the current
working directory won't affect the environment of the parent process.
You may find that under some Windows command line interpreters that the
change to the working directory made by a program persists into the
interactive session that triggered the program.

One way to achieve your desired goal, of course, is to call your program
using a shell expansion sequence (assuming Unix-like shell
environments), as in:

cd `myprogram.py`

and then if your program outputs the path to a directory your shell's
current working directory will be chaged as you require.

there's-always-a-way-ly y'rs - steve
--
Steve Holden http://www.holdenweb.com/
Python Web Programming http://pydish.holdenweb.com/
Holden Web LLC +1 703 861 4237 +1 800 494 3119
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.