navigating/changing directories

S

skip

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
 
K

Kartic

/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
 
K

Kartic

Hmmm... I take it back... that is not working! I think it changes the
path only during execution time.
 
N

Nick Coghlan

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.
 
S

Steve Holden

skip said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,968
Members
47,518
Latest member
TobiasAxf

Latest Threads

Top