Change directory not successfully done

S

Samuel Yin

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(command)
4) os.chdir(destdir)

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_name.
But what should I do in the case of python script?

Thanks and Regards

Samuel Yin
 
M

Mike Meyer

Samuel Yin said:
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(command)
4) os.chdir(destdir)

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_name.
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
 
B

Bengt Richter

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(command)
4) os.chdir(destdir)

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_name.
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,
'C:\\pywk\\grammar'


But if you execute 1) 2) and 4) by running mycd.py (comment out "3)") in
a separate process like os.system('python 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.,
... import os
... print 'changing cwd %s to parent =>'%(os.getcwd()),
... os.chdir('..')
... print os.getcwd()
... """)

Ok see where we are 'C:\\pywk\\grammar'

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

Check effect '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 ;-)
changing cwd C:\pywk to parent => C:\
0 'C:\\pywk'

I.e., changed but thrown away with subprocess

Does this help?

Regards,
Bengt Richter
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top