Batch commands on Windows

Dang Griffith noemail at noemail4u.com
Tue Jan 27 09:36:27 EST 2004


On Sat, 24 Jan 2004 04:23:40 GMT, "Moosebumps" <crap at crud.com> 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
>
If you're looking for more shell-like behavior, look into IPython at
http://ipython.scipy.org/.  Although it still won't "export" your
environment variables, or make your calls to os.system have a unique
environment, you can do things like 'ls' (you can probably configure
dir to work, but by default it provides access to the python builtin
function of that name).

Here is an excerpt that does some of the things in Mooosebump's
sample:

        In [15]: import os
        In [16]: os.environ['MYVAR'] = '3'
        In [17]: os.system('echo %MYVAR%')
        3
        Out[17]: 0
        In [18]: cd \
        C:\
        In [19]: ls /w/ad P*
         Volume in drive C has no label.
         Volume Serial Number is A096-107C

         Directory of C:\

        [Program Files] [Python23]
                       0 File(s)       0 bytes
                       2 Dir(s)  871,232 bytes free


Notice the unescaped backslash in line 18, and the "DOS" command line
options "/w/ad" and "P*" wildcard on line 19.

This still might not be what you're after, but it's a step in the
right direction.
    --dang



More information about the Python-list mailing list