How to run commands in command line from a script
mwm at mired.org
Fri Jul 1 15:42:15 CEST 2005
Peter Hansen <peter at engcorp.com> writes:
> Ivan Shevanski wrote:
>> Alright well I'm quite a noob and when I run a simple command to
>> change the current directory, nothing happens. I made a little test
>> script to show it:
>> import os
>> cwd = os.getcwd()
>> print cwd
>> os.system('cd = C:\Program Files')
>> print cwd
> 1. Commands like "cd" are executed by the shell, not as separate
> executables (.exe files). Unfortunately, the result of this is that
> their effects are lost when the shell returns to Python, so you can't
> do what you are trying to do in quite this manner.
It doesn't really matter that it's executed by the shell or as a
seperate exe. Things run via os.system don't happen in the Python
process, so any pre-process changes that they make won't show up in
the Python process.
Of course, shells suffer from the same problem. If they run another
process to make a per-process change, the change won't happen in the
shell process. So all the commands that make per-process changes -
like cd - are handled internally by the shell.
The trick is figuring out what things are per-process changes, and
what are global state changes, and then using the Python tools for
making the per-process changes. Since using the Python tools for
making global changes is usually more portable than invoking an
external command, you should try and use such tools for those changes
Mike Meyer <mwm at mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
More information about the Python-list