Batch commands on Windows

Rich Krauter rmkrauter at yahoo.com
Fri Jan 23 23:31:57 EST 2004


Each of your system calls spawns it's own separate shell with its own
set of environment variables. You probably want to look into os.environ
(python) or %ENV (perl) to set your shell variables.


On Fri, 2004-01-23 at 23:23, Moosebumps wrote:

> > Can you give an example of what you mean, in Perl as well as what you
> hoped
> > would work in Python? I couldn't quite understand what it is that you're
> trying
> > to do.
> 
> OK, actually on second test, the problem is mostly with IDLE, but not
> totally.  When I hit F5 under IDLE, it behaves differently with respect to
> the command window then if I just run it by double-clicking on the file.
> 
> Here is an example:
> 
> BatchTest.bat:
> 
> set MYVAR=3
> dir
> pause
> dir
> echo %MYVAR%
> pause
> 
> BatchTest.py:
> 
> # the intention is to do the same thing as BatchTest.bat, but it doesn't
> work under either IDLE or by double-clicking
> # in particular the environment variable is not saved, and it doesn't work
> if I replace os.system with os.popen
> 
> import os
> 
> os.system("set MYVAR=3")
> os.system("dir")
> os.system("pause")
> os.system("dir")
> os.system("echo %MYVAR%")
> os.system("pause")
> 
> BatchTest.pl:
> 
> # this actually does the same thing as Python, I was mistaken.  I was
> mislead by the IDLE behavior.
> 
> system('set MYVAR=3');
> system('dir');
> system('pause');
> system('dir');
> system('echo %MYVAR%');
> system('pause');
> 
> The general idea is that it would be nice if there weren't any differences
> between the batch file and python.  From a practical standpoint, it would
> encourage a lot of people to switch from nasty batch files to Python scripts
> if you could just surround the entire thing with os.batch(' ') or some
> similar sort of mechanical textual substitution.  Then you could clean it up
> gradually.
> 
> I am aware of os.environ and such, and that is useful, but it's not really
> the point.
> 
> Of course I could write a function to take a bunch of strings, write a batch
> file, save the working directory, execute it, restore the current directory,
> then delete the batch file, but that seems like an awful hack.  Though I
> probably will do that at some point.
> 
> > > What's the deal with that?  I thought Python started out as a scripting
> > > language.  And that seems like the most basic thing that a scripting
> > > language should do.
> >
> > Dunno, although MS-DOS shell scripting is certainly a small subset of
> scripting
> > in general. Maybe with a concrete example somebody will be able to give
> you a
> > hand.
> 
> It is a small subset, but an important subset.  Shell scripting started
> probably when people got sick of typing the same commands into the prompt.
> For a language to really support shell scripting, it should provide a way of
> automating the process of typing in the commands.  As in, there should be no
> difference whether you're actually typing, or you're running the script.
> 
> If there is a way and I don't know about it, I would be happy to hear about
> it.  But I do think it is a pretty big hole.
> 
> MB
> 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20040123/57f27ca8/attachment.html>


More information about the Python-list mailing list