473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

navigating/changing directories

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
5 3255
> /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
Hmmm... I take it back... that is not working! I think it changes the
path only during execution time.

Jul 18 '05 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MENTAT | last post by:
Hi, I am trying to create an installer for my web application. So I added a web setup project to my solution (I am using VS.NET 2003). Been playing around with it since then and it basically...
3
by: Paul Neave | last post by:
Yahoo! has launched a beta of it's new mapping application: http://maps.yahoo.com/beta/ It's based in Flash, but it uses JavaScript. I'm curious about one feature, though - when you pan about...
5
by: oddstray | last post by:
Hi, I'm using WinXP. I wrote a short C utility in which I want to change directories and run programs. When I use the system() method and pass in the same command that works in a cmd.exe...
4
by: Good Man | last post by:
Hi there I have a database with about 20 or so tables, maybe a few thousand rows in each. I am starting to do more complex things with my insertions etc, and I want to start to use...
1
by: JohnMOsborn | last post by:
I am designing an Access database that will use tab controls. Normally, you place different sets of fields on each page of the tab control – like Fields1-3 on Page 1, Fields 4-6 on Page 2, etc. In...
9
by: silverburgh.meryl | last post by:
i am trying to use python to walk thru each subdirectory from a top directory. Here is my script: savedPagesDirectory = "/home/meryl/saved_pages/data" dir=open(savedPagesDirectory, 'r') ...
0
by: Ohad Weiss | last post by:
Hi I have a problem with two textboxes binded to a dataset, based on paren table, and table for the child records. I can navigate between the master records (and of course the child records...
0
by: in10se | last post by:
I have a .NET 2.0 application that uses the WebBrowser control. Because all of my pages are generated dynamically, I am catching the Navigating event, cancelling it, and performing my own operations...
11
by: =?Utf-8?B?UGF1bA==?= | last post by:
I did a google search but could find what I was looking for. I am creating a temporary folder and placing files in it with a web application. The temporary folder name needs to be changed...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.