Current drive and directory

Duncan Booth me at privacy.net
Thu May 27 03:46:39 EDT 2004


Dennis Lee Bieber <wlfraed at ix.netcom.com> wrote in
news:56oab0lrlg9dit9g19qdrargpominsqkj0 at 4ax.com: 

>> And also, when I actually run the program, I have trouble using
>> notepad on files:
>> 
>> MS-DOS Prompt
>> 'Q' to Quit
>> 
>> C:\Documents and Settings\Erik\Desktop>cd \windows\system32
> 
>      This os.system() performed a CD, and then exited -- all history
> is lost.
> 
>> 
>> C:\Documents and Settings\Erik\Desktop>notepad calc.py
>>
>      This os.system() starts fresh, with the same settings as the
> parent program.
> 

Yes, that is what os.system does. It runs a single command, so it is 
roughly equivalent in this case to entering:

cmd /c cd \windows\system32
cmd /c notepad calc.py

at a windows command prompt. i.e. A separate shell is started, and 
terminated for each command.

To get what you want, you must execute the commands in the same shell. Some 
ways to do this:

For a short sequence of commands, just use the command separator as you 
would normally when entering multiple commands on one line at a command 
prompt. e.g.

  os.system(r"cd \windows\system32 && notepad calc.py")

For a longer sequence, you can write a temporary batch file then use 
os.system to execute it.

If you need more complex interaction you might want to use the popen2 
module to pipe commands in and read the output, but you will almost 
certainly need to use multiple threads if you try this otherwise your 
program will deadlock.

Or, for this specific case, just use absolute pathnames for everything so 
you don't have to change current directory before running a command.



More information about the Python-list mailing list