473,734 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Change directory not successfully done

Hi, guys,

This should be a simple problem, but I just can not resolve it. I just
want to use a python script to change my working directory. see my
following code:

# mycd.py
1) destdir = "xxxxxxxx"
2) command = "cd "+ destdir
3) os.system(comma nd)
4) os.chdir(destdi r)

But neither 3) nor 4) is used, I just can not change the directory after
execute mycd.py. This remind me of bash script. If you change directory
in your bash script file, it will only impact the sub process of that
script, except that you invoke that bash script by ./script_file_nam e.
But what should I do in the case of python script?

Thanks and Regards

Samuel Yin

Nov 11 '05 #1
2 2545
Samuel Yin <sa********@163 .com> writes:
Hi, guys,

This should be a simple problem, but I just can not resolve it. I just
want to use a python script to change my working directory. see my
following code:

# mycd.py
1) destdir = "xxxxxxxx"
2) command = "cd "+ destdir
3) os.system(comma nd)
4) os.chdir(destdi r)

But neither 3) nor 4) is used, I just can not change the directory after
execute mycd.py. This remind me of bash script. If you change directory
in your bash script file, it will only impact the sub process of that
script, except that you invoke that bash script by ./script_file_nam e.
But what should I do in the case of python script?


Actually, one solution is a level of indirection worse than the bash
script. Doing a cd changes the current directory of the process that
executes the cd system call. In a bash script, it's the shell
executing the script. In your python script, os.system launches
another process to run the command, and it's *that* process that has
it's directory changed. The os.chdir changes the shell of the python
interpreter, which still doesn't do you any good.

One solution is to switch to a shell that understands Python, and have
that execfile your script. There is a Python environment that can be
configured to be used as a shell, but I can't remeber it's name.

If you want to stay with bash, your solutions are the same as they are
for setting an environment variable in the parent shell. You can
google for that for a long discussion of the issues.

The solution I liked from that thread was an alias:

In your bash do: alias mycd="eval $(python mycd.py)"

mycd.py looks like:
destdir = 'xxxxxx'
command = 'os ' + destdir
print command

At the bash prompt you enter the command "mycd", your python script
builds a command for the shell to execute and prints it, the eval
reads that output and executes it in your shell.

If you want to pass arguments to the python script, you'll need to use
a shell function instead of an alias.

<mike
--
Mike Meyer <mw*@mired.or g> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 11 '05 #2
On Fri, 11 Nov 2005 10:16:02 +0800, Samuel Yin <sa********@163 .com> wrote:
Hi, guys,

This should be a simple problem, but I just can not resolve it. I just
want to use a python script to change my working directory. see my
following code:

# mycd.py
1) destdir = "xxxxxxxx"
2) command = "cd "+ destdir
3) os.system(comma nd)
4) os.chdir(destdi r)

But neither 3) nor 4) is used, I just can not change the directory after
execute mycd.py. This remind me of bash script. If you change directory I think you are reminded well. It is no use to create a throwaway child process
that has the working directory you want, but is just thrown away along with the process ;-)
in your bash script file, it will only impact the sub process of that
script, except that you invoke that bash script by ./script_file_nam e.
But what should I do in the case of python script?

If you do 1) 2) and 4) _within_ your script, the process that is running
your script should see the new working directory. E.g., interactively,
import os
os.getcwd() 'C:\\pywk\\gram mar\\ast' os.chdir('..')
os.getcwd() 'C:\\pywk\\gram mar'
But if you execute 1) 2) and 4) by running mycd.py (comment out "3)") in
a separate process like os.system('pyth on mycd.py') or such, that's only
going to throw it away. If you really want the directory change as a script
that will affect your running script, import it or execfile it. E.g.,
open('mycd.py', 'w').write("""\ ... import os
... print 'changing cwd %s to parent =>'%(os.getcwd( )),
... os.chdir('..')
... print os.getcwd()
... """)

Ok see where we are import os
os.getcwd() 'C:\\pywk\\gram mar'

Now see if importing the module we just wrote will do what it's supposed to
import mycd changing cwd C:\pywk\grammar to parent => C:\pywk

Check effect os.getcwd() 'C:\\pywk'

Seems like it worked.

But note:
(BTW we now have to specify the old subdirectory
from current working dir in order to reach mycd.py ;-)
os.system('py24 grammar\\mycd.p y') changing cwd C:\pywk to parent => C:\
0 os.getcwd()

'C:\\pywk'

I.e., changed but thrown away with subprocess

Does this help?

Regards,
Bengt Richter
Nov 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
7957
by: Raseliarison nirinA | last post by:
hi all, i found an unanswered question at http://www.faqts.com/knowledge_base/index.phtml/fid/538 with possible response below. i've tried to send it at faqt.python but can't figure out how to edit the page. so i put it here. i want to kwon if this can convert all wave file. is there other encodage than 8 or 16 bits for .wav files? any bug and comment are welcome --
4
2634
by: Richard Cornford | last post by:
For the last couple of months I have been trying to get the next round of updates to the FAQ underway and been being thwarted by a heavy workload (the project I am working on has to be finished an QA tested for a new year release. I don't think that going to prove practical, but there is no harm in trying :) and some serious family commitments. But it has to be done soon so this is stage one. Mike Winter provided an extensive list of...
0
2536
by: No Spam | last post by:
After looking all the ways to progrrammatically create a new FTP Server & Virtual Directory, I have come to see that the code below is the cleanest. Here is my basic issue. When I run this code, I successfully create a new FTP Server and virtual directory. The strange this is that the description of the new server is (Stopped) and the status has the following warning "The specified metadata was not found.". If I manually Stop the...
2
5491
by: yxq | last post by:
Hello I want to create and delete the folder share, i found that it is ok for generic folder, but it does not work for Root directory(i.e c:\, d:\) The code **************************************************** Dim mc As New ManagementClass("Win32_Share") Dim mo As ManagementObject Dim inParams As ManagementBaseObject Dim outParams As ManagementBaseObject
18
23791
by: Arthur | last post by:
Hi All, I would like to get the name of the user given their networkID, is this something Active Directory would be useful for?(For intranet users) If so, can you please point me to some sample code/examples? Thanks in advance, Arthur
3
3188
by: Chris Mellon | last post by:
This appears to be a change in behavior from Python 2.4 to Python 2.5, which I can't find documented anywhere. It may be windows only, or related to Windows behavior. In 2.4, the current directory (os.curdir) was on sys.path. In 2.5, it appears to be the base directory of the running script. For example, if you execute the file testme.py in your current working directory, '' is on sys.path. If you execute c:\Python25\Scripts\testme.py,...
11
4100
by: raylopez99 | last post by:
Keep in mind this is my first compiled SQL program Stored Procedure (SP), copied from a book by Frasier Visual C++.NET in Visual Studio 2005 (Chap12). So far, so theory, except for one bug (feature?) below. At some point I'm sure I'll be able to laugh about this, akin to forgeting a semi-colon in C/C++, but right now it's frustrating (time to sleep on it for a while). Problem-- For some reason I get the error when trying to save files...
0
2215
by: shellegreen | last post by:
Hello, I need help with some password changes that I have to do in some iPAQs. The company that I work has 3 models of iPAQ and 3 different versions of Windows Mobile (Microsoft Pocket PC 4.20.0, Win Mobile 2003 SE e Win Mobile 5.0). The devices are configured to use Dial up or Cell Phone connection (Bluetooth or infrared). Recently we developed a program that check the user password expiration date in Active Directory Server every...
5
4989
by: =?Utf-8?B?UGVy?= | last post by:
I want to change the current path in the OpenFileDialog in runtime. When the user presses OK-button I'm checking the selected path if it's valid for my application. If it's not valid I want to set the path to a valid one in the FileOk-event and set the e.Cancel = true.
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.